Last active
December 11, 2015 23:58
-
-
Save tacksoo/4680050 to your computer and use it in GitHub Desktop.
Multithreaded Java program that counts how many times a certain word appears in a text file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
import javax.swing.JTextArea; | |
import javax.swing.SwingUtilities; | |
public class WordCountRunnable implements Runnable { | |
private String word; | |
private JTextArea textArea; | |
private int count = 0; | |
public WordCountRunnable(String word, JTextArea textArea) { | |
this.word = word; | |
this.textArea = textArea; | |
} | |
@Override | |
public void run() { | |
File f = new File("docs/pg2600.txt"); | |
try { | |
Scanner scanner = new Scanner(f); | |
while (scanner.hasNext()) | |
{ | |
String str = scanner.next(); | |
if (str.equals(word)) | |
count++; | |
} | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
textArea.append(word + " appears: " + count + " times\n"); | |
//System.out.println(SwingUtilities.isEventDispatchThread()); | |
} | |
}); | |
scanner.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.GridLayout; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
import javax.swing.JTextField; | |
import javax.swing.SwingUtilities; | |
public class WordsCounter { | |
private JTextField firstTextField; | |
private JTextField secondTextField; | |
private JTextField thirdTextField; | |
private JTextArea textArea; | |
private JButton startButton; | |
private JButton clearButton; | |
public WordsCounter() { | |
JFrame frame = new JFrame("WordsCounter"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.add(makeLabelsPanel(), BorderLayout.NORTH); | |
frame.add(makeTextAreaPanel(), BorderLayout.CENTER); | |
frame.add(makeButtonPanel(), BorderLayout.SOUTH); | |
frame.setSize(300, 300); | |
frame.setVisible(true); | |
// System.out.println(SwingUtilities.isEventDispatchThread()); | |
} | |
private JPanel makeLabelsPanel() { | |
JPanel panel = new JPanel(); | |
panel.setLayout(new GridLayout(3, 2)); | |
firstTextField = new JTextField(10); | |
secondTextField = new JTextField(10); | |
thirdTextField = new JTextField(10); | |
panel.add(new JLabel("Please enter word")); | |
panel.add(firstTextField); | |
panel.add(new JLabel("Please enter word")); | |
panel.add(secondTextField); | |
panel.add(new JLabel("Please enter word")); | |
panel.add(thirdTextField); | |
return panel; | |
} | |
private JScrollPane makeTextAreaPanel() { | |
JPanel panel = new JPanel(); | |
textArea = new JTextArea(); | |
panel.setLayout(new BorderLayout()); | |
panel.add(textArea); | |
JScrollPane scrollPane = new JScrollPane(panel); | |
return scrollPane; | |
} | |
private JPanel makeButtonPanel() { | |
JPanel panel = new JPanel(); | |
startButton = new JButton("Start"); | |
clearButton = new JButton("Clear"); | |
panel.add(startButton); | |
panel.add(clearButton); | |
startButton.addActionListener(new StartListener()); | |
clearButton.addActionListener(new ClearListener()); | |
return panel; | |
} | |
public class StartListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (!firstTextField.getText().equals("")) { | |
new Thread(new WordCountRunnable(firstTextField.getText(), textArea)).start(); | |
} | |
if (!secondTextField.getText().equals("")) { | |
new Thread(new WordCountRunnable(secondTextField.getText(), textArea)).start(); | |
} | |
if (!thirdTextField.getText().equals("")) { | |
new Thread(new WordCountRunnable(thirdTextField.getText(), textArea)).start(); | |
} | |
} | |
} | |
public class ClearListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent arg0) { | |
firstTextField.setText(""); | |
secondTextField.setText(""); | |
thirdTextField.setText(""); | |
} | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
new WordsCounter(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment