Advertisement
Guest User

Untitled

a guest
Nov 19th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. /*WordCount.java
  2. Application determines how many times a word has occurred in the text given
  3. */
  4. import javax.swing.JPanel;
  5. import javax.swing.JFrame;
  6. import javax.swing.JButton;
  7. import javax.swing.JTextArea;
  8. import javax.swing.JLabel;
  9. import java.awt.BorderLayout;
  10. import java.awt.GridLayout;
  11. import javax.swing.JScrollPane;
  12. import java.awt.Insets;
  13. import java.awt.Color;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.util.Map;
  17. import java.util.HashMap;
  18. import java.util.Set;
  19. import java.util.TreeSet;
  20. import java.util.StringTokenizer;
  21. public class WordCount extends JFrame{
  22. private JPanel buttonsPanel;
  23. private JPanel topPanel;
  24. private JTextArea textInput;
  25. private JTextArea textOutput;
  26. private JButton processButton;
  27. private JButton clearButton;
  28. private JLabel instructions;
  29. private BorderLayout frameLayout;
  30. private BorderLayout topPanelLayout;
  31. private GridLayout buttonsPanelLayout;
  32. private String stringToTest;//holds string pasted in textarea
  33. private String[] tokens;//array of tokenized strings
  34. Map<String,Integer> mp = new HashMap<String, Integer>();//declaring a map object
  35. //constructor
  36. public WordCount(){
  37. super("Word count application");
  38. JPanel buttonsPanel = new JPanel();//holds the two buttons
  39. JPanel topPanel = new JPanel();//holds the textArea and jlabel
  40. JTextArea textInput = new JTextArea(10,15);//holds the input text
  41. JTextArea textOutput = new JTextArea(10,15);//hold the output text
  42. stringToTest = "";//initialize to empty string
  43. JButton processButton = new JButton("Count");//action abutton
  44. JButton clearButton = new JButton("Clear");//to clear textArea
  45. JLabel instructions = new JLabel("Copy and paste text in the below text area, click Count to count the words.\n");//holding instructions
  46. BorderLayout frameLayout = new BorderLayout();//for JFrame
  47. BorderLayout topPanelLayout = new BorderLayout();//for top level panel
  48. GridLayout buttonsPanelLayout = new GridLayout(1,2,5,5);//for buttons
  49. setLayout(frameLayout);//set layout of Frame
  50. topPanel.setLayout(topPanelLayout);//set topPanel layout
  51. topPanel.add(instructions, BorderLayout.NORTH);//add JLabel to topPanel
  52. instructions.setOpaque(true);//need this to be able to change the colour
  53. instructions.setBackground(Color.CYAN);
  54. topPanel.add(new JScrollPane(textInput), BorderLayout.CENTER);//add textInput to topPanel
  55. buttonsPanel.setLayout(buttonsPanelLayout);//set buttonsPanel layout
  56. buttonsPanel.add(processButton);//add processButton to panel
  57. buttonsPanel.add(clearButton);//add clearButton to panel
  58. add(topPanel, BorderLayout.NORTH);//add topPanel to JFrame
  59. add(buttonsPanel, BorderLayout.CENTER);//add buttonsPanel to JFrame
  60. add(new JScrollPane(textOutput), BorderLayout.SOUTH);//add textOutput to JFrame
  61. textOutput.setMargin(new Insets(5,5,5,5));//sets margins inside the element
  62. textInput.setMargin(new Insets(5,5,5,5));//sets margins inside the element
  63. textInput.setText(" ");//initialize to empty string
  64. textOutput.setText(" ");//initialize to empty string
  65. ProcessButtonHandling handler1 = new ProcessButtonHandling();
  66. ClearButtonHandling handler2 = new ClearButtonHandling();
  67. processButton.addActionListener(handler1);
  68. clearButton.addActionListener(handler2);
  69. }//end of WordCount constructor
  70. public String getString(){
  71. return stringToTest;
  72. }
  73. //inner class for event handlings
  74. private class ProcessButtonHandling implements ActionListener{
  75. public void actionPerformed(ActionEvent event){
  76. stringToTest = textInput.getText();
  77. processString(getString());//call function to split string
  78. mapWords();//count words and place results in the hashmap
  79. printMap();//print the map values and keys in the textArea
  80. }//end of actionPerformed
  81. }//end of inner class
  82. private class ClearButtonHandling implements ActionListener{
  83. public void actionPerformed(ActionEvent event){
  84.  
  85. }//end of actionPerformed
  86. }//end of inner class
  87. public void processString(String testingString){
  88. //String[] tokens = testingString.split("(?=[,.])|\\s+");
  89. testingString = testingString.replaceAll("[,.;:]","");//remove the characters in brackets and replace them with nothing
  90. tokens = testingString.split(" ");//split string using space as delimiter
  91. /* System.out.printf("Number of elements: %d\nTokens are \n", tokens.length);
  92. //print to test
  93. for(String token : tokens){
  94. System.out.println(token);
  95. } */
  96. }
  97. public void mapWords(){
  98. for(int i = 0; i < tokens.length; i++){
  99. if(mp.containsKey(tokens[i])){
  100. //increment Integer
  101. mp.put(tokens[i], mp.get(tokens[i]) + 1);//add it to the hashmap and increment integer
  102. }
  103. else{
  104. mp.put(tokens[i],1);//add it to the hashmap
  105. }
  106. }
  107. }
  108. public void printMap(){
  109. for(Map.Entry<String, Integer> entry : mp.entrySet()){//loop thru map to print data
  110. //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  111. textOutput.append("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  112. }
  113. }
  114.  
  115. }//end of WordCount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement