HELP please im STUCK!

Reply

Join Date: Aug 2006
Posts: 4
Reputation: pfr is an unknown quantity at this point 
Solved Threads: 0
pfr's Avatar
pfr pfr is offline Offline
Newbie Poster

HELP please im STUCK!

 
0
  #1
Aug 12th, 2006
Hi
this is my first time posting here

i was working on a text editor that can use

1.total number of words
2.number of unique words (not case-sensitive, ie 'Table' and 'table' are the same word)
3.the word with the highest occurrence
4.the list of unique punctuations used in the text file (, . ; : ? !)

I gotten some of it down so please give help and advice and correct my code please. im having trouble with basically all them.
Please help me!
run the program

  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.util.*;
  6.  
  7. public class TextEditor extends JApplet {
  8.  
  9.  
  10. // Create menu items
  11. private JMenuItem jmiOpen = new JMenuItem("Open");
  12. private JMenuItem jmiSave = new JMenuItem("Save");
  13. private JMenuItem jmiClear = new JMenuItem("Clear");
  14. private JMenuItem jmiCount = new JMenuItem("Count Words");
  15. private JMenuItem jmiExit = new JMenuItem("Exit");
  16. private JMenuItem jmiForeground = new JMenuItem("Foreground");
  17. private JMenuItem jmiBackground = new JMenuItem("Background");
  18.  
  19. // Create buttons to be placed in a tool bar
  20. private JButton jbtOpen = new JButton();
  21. private JButton jbtSave = new JButton();
  22. private JLabel jlblStatus = new JLabel();
  23.  
  24. // Create a JFileChooser with the current directory
  25. private JFileChooser jFileChooser1
  26. = new JFileChooser(new File("."));
  27.  
  28. // Create a text area
  29. private JTextArea jta = new JTextArea();
  30.  
  31. public TextEditor() {
  32. // Add menu items to the menu
  33. JMenu jMenu1 = new JMenu("File");
  34. jMenu1.add(jmiOpen);
  35. jMenu1.add(jmiSave);
  36. jMenu1.add(jmiClear);
  37. jMenu1.add(jmiCount);
  38. jMenu1.addSeparator();
  39. jMenu1.add(jmiExit);
  40.  
  41. // Add menu items to the menu
  42. JMenu jMenu2 = new JMenu("Edit");
  43. jMenu2.add(jmiForeground);
  44. jMenu2.add(jmiBackground);
  45.  
  46. // Add menus to the menu bar
  47. JMenuBar jMenuBar1 = new JMenuBar();
  48. jMenuBar1.add(jMenu1);
  49. jMenuBar1.add(jMenu2);
  50.  
  51. // Set the menu bar
  52. setJMenuBar(jMenuBar1);
  53.  
  54. // Create tool bar
  55. JToolBar jToolBar1 = new JToolBar();
  56. jToolBar1.add(jbtOpen);
  57. jToolBar1.add(jbtSave);
  58.  
  59. jmiOpen.addActionListener(new ActionListener() {
  60. public void actionPerformed(ActionEvent e) {
  61. open();
  62. }
  63. });
  64.  
  65. jmiSave.addActionListener(new ActionListener() {
  66. public void actionPerformed(ActionEvent evt) {
  67. save();
  68. }
  69. });
  70. jmiCount.addActionListener(new ActionListener() {
  71. public void actionPerformed(ActionEvent evt) {
  72. countWords();
  73. }
  74. });
  75. jmiClear.addActionListener(new ActionListener() {
  76. public void actionPerformed(ActionEvent evt) {
  77. jta.setText(null);
  78. }
  79. });
  80.  
  81. jmiExit.addActionListener(new ActionListener() {
  82. public void actionPerformed(ActionEvent evt) {
  83. System.exit(0);
  84. }
  85. });
  86.  
  87. jmiForeground.addActionListener(new ActionListener() {
  88. public void actionPerformed(ActionEvent evt) {
  89. Color selectedColor =
  90. JColorChooser.showDialog(null, "Choose Foreground Color",
  91. jta.getForeground());
  92.  
  93. if (selectedColor != null)
  94. jta.setForeground(selectedColor);
  95. }
  96. });
  97.  
  98. jmiBackground.addActionListener(new ActionListener() {
  99. public void actionPerformed(ActionEvent evt) {
  100. Color selectedColor =
  101. JColorChooser.showDialog(null, "Choose Background Color",
  102. jta.getForeground());
  103.  
  104. if (selectedColor != null)
  105. jta.setBackground(selectedColor);
  106. }
  107. });
  108.  
  109. jbtOpen.addActionListener(new ActionListener() {
  110. public void actionPerformed(ActionEvent evt) {
  111. open();
  112. }
  113. });
  114.  
  115. jbtSave.addActionListener(new ActionListener() {
  116. public void actionPerformed(ActionEvent evt) {
  117. save();
  118. }
  119. });
  120.  
  121. getContentPane().add(jToolBar1, BorderLayout.NORTH);
  122. getContentPane().add(jlblStatus, BorderLayout.SOUTH);
  123. getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
  124. }
  125.  
  126. /** Open file */
  127. private void open() {
  128. if (jFileChooser1.showOpenDialog(this) ==
  129. JFileChooser.APPROVE_OPTION)
  130. open(jFileChooser1.getSelectedFile());
  131. }
  132.  
  133. /** Open file with the specified File instance */
  134. private void open(File file) {
  135. try {
  136. // Read from the specified file and store it in jta
  137. BufferedInputStream in = new BufferedInputStream(
  138. new FileInputStream(file));
  139. byte[] b = new byte[in.available()];
  140. in.read(b, 0, b.length);
  141. jta.append(new String(b, 0, b.length));
  142. in.close();
  143.  
  144. // Display the status of the Open file operation in jlblStatus
  145. jlblStatus.setText(file.getName() + " Opened");
  146. }
  147. catch (IOException ex) {
  148. jlblStatus.setText("Error opening " + file.getName());
  149. }
  150. }
  151.  
  152. /** Save file */
  153. private void save() {
  154. if (jFileChooser1.showSaveDialog(this) ==
  155. JFileChooser.APPROVE_OPTION) {
  156. save(jFileChooser1.getSelectedFile());
  157. }
  158. }
  159.  
  160. private void countWords()
  161. {
  162. int numwords;
  163. String text = jta.getText();
  164.  
  165. // Scanner scan = new Scanner(text);
  166. //scan.useDelimiter("");
  167.  
  168. // while (scan.hasNext());
  169. // System.out.println(scan.next());
  170.  
  171. // Create a hash map to hold words as key and count as value
  172. Map<String, Integer> hashMap = new HashMap<String, Integer>();
  173.  
  174. String[] words = text.split("[ .!?\\n-]");
  175. numwords = words.length;
  176. System.out.println("Number of words: " + numwords);
  177.  
  178. for (int i = 0; i < words.length; i++) {
  179. if (words[i].length() > 1) {
  180. if (hashMap.get(words[i]) != null) {
  181. int value = hashMap.get(words[i]).intValue();
  182. value++;
  183. hashMap.put(words[i], value);
  184. }
  185. else
  186. hashMap.put(words[i], 1);
  187. }
  188. }
  189.  
  190. // Create a tree map from the hash map
  191. Map<String, Integer> treeMap =
  192. new TreeMap<String, Integer>(hashMap);
  193.  
  194. // Display mappings
  195. System.out.println("Display words and their count in " +
  196. "ascending order of the words");
  197. System.out.print(treeMap);
  198.  
  199. }
  200.  
  201. /** Save file with specified File instance */
  202. private void save(File file) {
  203. try {
  204. // Write the text in jta to the specified file
  205. BufferedOutputStream out = new BufferedOutputStream(
  206. new FileOutputStream(file));
  207. byte[] b = (jta.getText()).getBytes();
  208. out.write(b, 0, b.length);
  209. out.close();
  210.  
  211. // Display the status of the save file operation in jlblStatus
  212. jlblStatus.setText(file.getName() + " Saved ");
  213. }
  214. catch (IOException ex) {
  215. jlblStatus.setText("Error saving " + file.getName());
  216. }
  217. }
  218.  
  219. public static void main(String[] args) {
  220. TextEditor applet = new TextEditor();
  221. JFrame frame = new JFrame();
  222. //EXIT_ON_CLOSE == 3
  223. frame.setDefaultCloseOperation(3);
  224. frame.setTitle("TextEditor");
  225. frame.getContentPane().add(applet, BorderLayout.CENTER);
  226. applet.init();
  227. applet.start();
  228. frame.setSize(400,320);
  229. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  230. frame.setLocation((d.width - frame.getSize().width) / 2,
  231. (d.height - frame.getSize().height) / 2);
  232. frame.setVisible(true);
  233. }
  234. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: HELP please im STUCK!

 
0
  #2
Aug 12th, 2006
Take a look at the StringTokenizer or Regex packages and then get back if you still have questions. You should be able to specify white space as a delimiter and quickly be able to count the number of words.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC