Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*WordCount.java
- Application determines how many times a word has occurred in the text given
- */
- import javax.swing.JPanel;
- import javax.swing.JFrame;
- import javax.swing.JButton;
- import javax.swing.JTextArea;
- import javax.swing.JLabel;
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import javax.swing.JScrollPane;
- import java.awt.Insets;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Set;
- import java.util.TreeSet;
- import java.util.StringTokenizer;
- public class WordCount extends JFrame{
- private JPanel buttonsPanel;
- private JPanel topPanel;
- private JTextArea textInput;
- private JTextArea textOutput;
- private JButton processButton;
- private JButton clearButton;
- private JLabel instructions;
- private BorderLayout frameLayout;
- private BorderLayout topPanelLayout;
- private GridLayout buttonsPanelLayout;
- private String stringToTest;//holds string pasted in textarea
- private String[] tokens;//array of tokenized strings
- Map<String,Integer> mp = new HashMap<String, Integer>();//declaring a map object
- //constructor
- public WordCount(){
- super("Word count application");
- JPanel buttonsPanel = new JPanel();//holds the two buttons
- JPanel topPanel = new JPanel();//holds the textArea and jlabel
- JTextArea textInput = new JTextArea(10,15);//holds the input text
- JTextArea textOutput = new JTextArea(10,15);//hold the output text
- stringToTest = "";//initialize to empty string
- JButton processButton = new JButton("Count");//action abutton
- JButton clearButton = new JButton("Clear");//to clear textArea
- JLabel instructions = new JLabel("Copy and paste text in the below text area, click Count to count the words.\n");//holding instructions
- BorderLayout frameLayout = new BorderLayout();//for JFrame
- BorderLayout topPanelLayout = new BorderLayout();//for top level panel
- GridLayout buttonsPanelLayout = new GridLayout(1,2,5,5);//for buttons
- setLayout(frameLayout);//set layout of Frame
- topPanel.setLayout(topPanelLayout);//set topPanel layout
- topPanel.add(instructions, BorderLayout.NORTH);//add JLabel to topPanel
- instructions.setOpaque(true);//need this to be able to change the colour
- instructions.setBackground(Color.CYAN);
- topPanel.add(new JScrollPane(textInput), BorderLayout.CENTER);//add textInput to topPanel
- buttonsPanel.setLayout(buttonsPanelLayout);//set buttonsPanel layout
- buttonsPanel.add(processButton);//add processButton to panel
- buttonsPanel.add(clearButton);//add clearButton to panel
- add(topPanel, BorderLayout.NORTH);//add topPanel to JFrame
- add(buttonsPanel, BorderLayout.CENTER);//add buttonsPanel to JFrame
- add(new JScrollPane(textOutput), BorderLayout.SOUTH);//add textOutput to JFrame
- textOutput.setMargin(new Insets(5,5,5,5));//sets margins inside the element
- textInput.setMargin(new Insets(5,5,5,5));//sets margins inside the element
- textInput.setText(" ");//initialize to empty string
- textOutput.setText(" ");//initialize to empty string
- ProcessButtonHandling handler1 = new ProcessButtonHandling();
- ClearButtonHandling handler2 = new ClearButtonHandling();
- processButton.addActionListener(handler1);
- clearButton.addActionListener(handler2);
- }//end of WordCount constructor
- public String getString(){
- return stringToTest;
- }
- //inner class for event handlings
- private class ProcessButtonHandling implements ActionListener{
- public void actionPerformed(ActionEvent event){
- stringToTest = textInput.getText();
- processString(getString());//call function to split string
- mapWords();//count words and place results in the hashmap
- printMap();//print the map values and keys in the textArea
- }//end of actionPerformed
- }//end of inner class
- private class ClearButtonHandling implements ActionListener{
- public void actionPerformed(ActionEvent event){
- }//end of actionPerformed
- }//end of inner class
- public void processString(String testingString){
- //String[] tokens = testingString.split("(?=[,.])|\\s+");
- testingString = testingString.replaceAll("[,.;:]","");//remove the characters in brackets and replace them with nothing
- tokens = testingString.split(" ");//split string using space as delimiter
- /* System.out.printf("Number of elements: %d\nTokens are \n", tokens.length);
- //print to test
- for(String token : tokens){
- System.out.println(token);
- } */
- }
- public void mapWords(){
- for(int i = 0; i < tokens.length; i++){
- if(mp.containsKey(tokens[i])){
- //increment Integer
- mp.put(tokens[i], mp.get(tokens[i]) + 1);//add it to the hashmap and increment integer
- }
- else{
- mp.put(tokens[i],1);//add it to the hashmap
- }
- }
- }
- public void printMap(){
- for(Map.Entry<String, Integer> entry : mp.entrySet()){//loop thru map to print data
- //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- textOutput.append("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- }
- }
- }//end of WordCount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement