PLS Help newbie

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 2
Reputation: serkanozgen is an unknown quantity at this point 
Solved Threads: 0
serkanozgen serkanozgen is offline Offline
Newbie Poster

PLS Help newbie

 
0
  #1
Jan 3rd, 2005
How can i add reversing line code to the following code. Program must read the document which is selected by user from filechooser and write the reverse lines to another file. Additionally, I have to add a histogram which shows the length of each lines in document.

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import javax.swing.*;
  5. import java.text.*;
  6.  
  7. public class WordAnalyser extends JFrame implements ActionListener
  8. {
  9. // Menu items Open, Clear, Exit, WordCount, About
  10. private JMenuItem jmiOpen, jmiClear, jmiExit, jmiWordCount, jmiAbout;
  11.  
  12. // Text area for displaying and editing text files
  13. private JTextArea jta1, jta2;
  14.  
  15. // Status label for displaying operation status
  16. private JLabel jlblStatus;
  17.  
  18. // File dialog box
  19. private JFileChooser jFileChooser = new JFileChooser();
  20. File infile;
  21. File outfile = new File("OutFile.txt");
  22.  
  23. DecimalFormat numForm1 = new DecimalFormat("000");
  24. DecimalFormat numForm2 = new DecimalFormat("0.00");
  25.  
  26. // Main method
  27. public static void main(String[] args)
  28. {
  29. WordAnalyser frame = new WordAnalyser();
  30. frame.setSize(500, 400);
  31. frame.center();
  32. frame.setVisible(true);
  33. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34. }
  35.  
  36. public WordAnalyser()
  37. {
  38. setTitle("Word Analyser");
  39.  
  40. // Create a menu bar mb and attach to the frame
  41. JMenuBar mb = new JMenuBar();
  42. setJMenuBar(mb);
  43.  
  44. // Add a "File" menu in mb
  45. JMenu fileMenu = new JMenu("File");
  46. fileMenu.setMnemonic('F');
  47. mb.add(fileMenu);
  48.  
  49. // Add a "Help" menu in mb
  50. JMenu helpMenu = new JMenu("Help");
  51. helpMenu.setMnemonic('H');
  52. mb.add(helpMenu);
  53.  
  54. // Create and add menu items to the menu
  55. fileMenu.add(jmiOpen = new JMenuItem("Open", 'O'));
  56. fileMenu.add(jmiClear = new JMenuItem("Clear", 'C'));
  57. fileMenu.addSeparator();
  58. fileMenu.add(jmiExit = new JMenuItem("Exit", 'E'));
  59. helpMenu.add(jmiWordCount = new JMenuItem("Word Count", 'W'));
  60. helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
  61.  
  62. // Set default directory to the current directory
  63. jFileChooser.setCurrentDirectory(new File("."));
  64.  
  65. // Set BorderLayout for the frame
  66. getContentPane().add(new JScrollPane(jta1 = new JTextArea()), BorderLayout.CENTER);
  67. getContentPane().add(jta2 = new JTextArea(), BorderLayout.EAST);
  68. getContentPane().add(jlblStatus = new JLabel(), BorderLayout.SOUTH);
  69.  
  70. // Register listeners
  71. jmiOpen.addActionListener(this);
  72. jmiClear.addActionListener(this);
  73. jmiExit.addActionListener(this);
  74. jmiWordCount.addActionListener(this);
  75. jmiAbout.addActionListener(this);
  76. }
  77.  
  78.  
  79. public void center()
  80. {
  81. // Get the screen dimension
  82. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  83. int screenWidth = screenSize.width;
  84. int screenHeight = screenSize.height;
  85.  
  86. // Get the frame dimension
  87. Dimension frameSize = this.getSize();
  88. int x = (screenWidth - frameSize.width)/2;
  89. int y = (screenHeight - frameSize.height)/2;
  90.  
  91. // Determine the location of the left corner of the frame
  92. if (x < 0)
  93. {
  94. x = 0;
  95. frameSize.width = screenWidth;
  96. }
  97.  
  98. if (y < 0)
  99. {
  100. y = 0;
  101. frameSize.height = screenHeight;
  102. }
  103.  
  104. // Set the frame to the specified location
  105. this.setLocation(x, y);
  106. }
  107.  
  108.  
  109. // Handle ActionEvent for menu items
  110. public void actionPerformed(ActionEvent e)
  111. {
  112. String actionCommand = e.getActionCommand();
  113.  
  114. if (e.getSource() instanceof JMenuItem)
  115. {
  116. if ("Open".equals(actionCommand))
  117. open();
  118. else if ("Clear".equals(actionCommand))
  119. clear();
  120. else if ("Exit".equals(actionCommand))
  121. System.exit(0);
  122. else if ("Word Count".equals(actionCommand))
  123. word_count();
  124. else if ("About".equals(actionCommand))
  125. JOptionPane.showMessageDialog(this,
  126. "Program performs text analysis",
  127. "About This Program",
  128. JOptionPane.INFORMATION_MESSAGE);
  129. }
  130. }
  131.  
  132. // Open file
  133. private void open()
  134. {
  135. if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
  136. {
  137. infile = jFileChooser.getSelectedFile();
  138. open(infile);
  139. }
  140. }
  141.  
  142. // Open file with the specified File instance
  143. private void open(File file)
  144. {
  145. try
  146. {
  147. // Read from the specified file and store it in jta
  148. BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
  149. byte[] b = new byte[in.available()];
  150. in.read(b, 0, b.length);
  151. jta1.append(new String(b, 0, b.length));
  152. in.close();
  153.  
  154. // Display the status of the Open file operation in jlblStatus
  155. jlblStatus.setText(file.getName() + " Opened");
  156. }
  157. catch (IOException ex)
  158. {
  159. jlblStatus.setText("Error opening " + file.getName());
  160. }
  161. }
  162.  
  163. // Clear all display areas
  164. private void clear()
  165. {
  166. jta1.setText("");
  167. jta2.setText("");
  168. jlblStatus.setText("");
  169.  
  170. }
  171.  
  172. // Perform word analysis with specified File instance
  173. private void word_count()
  174. {
  175. int buff; // for reading 1 char at a time
  176. int count = 0;
  177. int sentences = 0;
  178. int words = 0;
  179. int chars = 0;
  180. boolean start = true;
  181.  
  182. try
  183. {
  184. FileInputStream instream = new FileInputStream(infile);
  185.  
  186. FileOutputStream outstream = new FileOutputStream(outfile);
  187.  
  188. // convert FileOutputSream object to PrintStream object for easier output
  189. PrintStream out = new PrintStream(outstream);
  190.  
  191. out.println("---Word Analysis---");
  192.  
  193. while ((buff=instream.read()) != -1)
  194. {
  195. switch((char)buff)
  196. {
  197. case '?': case '.': case '!':
  198. {
  199. if (start == false)
  200. {
  201. sentences++;
  202. words++;
  203. start = true;
  204. }
  205. break;
  206. }
  207.  
  208. case ' ': case '\t': case '\n': case ',': case ';': case ':': case'\"': case'\'':
  209. {
  210. if (start == false)
  211. {
  212. words++;
  213. start = true;
  214. }
  215. break;
  216. }
  217.  
  218. default:
  219. {
  220. // 3-digit integer format
  221.  
  222. if (((char)buff >= 'a' && (char)buff<='z')||
  223. ((char)buff >= 'A' && (char)buff<='Z')||
  224. ((char)buff >= '0' && (char)buff <= '9')||
  225. ((char)buff == '-'))
  226. {
  227. chars++;
  228. if ((words % 50) == 49)
  229. {
  230. if (start == true)
  231. {
  232. out.println();
  233. out.print(numForm1.format(words+1) + " ");
  234. }
  235. out.print((char)buff);
  236. }
  237.  
  238. start = false;
  239. }
  240. }
  241. }// switch
  242. }//while
  243.  
  244. instream.close();
  245.  
  246. out.println();
  247. out.println();
  248. out.println("Number of characters: " + chars);
  249. out.println("Number of words: " + words);
  250. out.println("Number of sentences: " + sentences);
  251.  
  252. out.print("Number of words per sentence: ");
  253.  
  254. // cast integers to float, then add 0.005 to round up to 2 decimal places
  255. out.println(numForm2.format((float)words/sentences));
  256.  
  257. outstream.close();
  258.  
  259. try
  260. {
  261. //Read from the output file and display in jta
  262. BufferedInputStream in = new BufferedInputStream(new FileInputStream(outfile));
  263. byte[] b = new byte[in.available()];
  264. in.read(b, 0, b.length);
  265. jta2.append(new String(b, 0, b.length));
  266. in.close();
  267. }
  268. catch (IOException ex)
  269. {
  270. jlblStatus.setText("Error opening " + outfile.getName());
  271. }
  272. }
  273. catch (Exception e)
  274. {
  275. System.out.println(e);
  276. }
  277. }
  278. }
Last edited by alc6379; Jan 3rd, 2005 at 5:56 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: PLS Help newbie

 
0
  #2
Jan 3rd, 2005
Hi everyone,

Use the getText() in the text area and get the length of the string. After that convert the string into a chars array and then reverse the char array adding the reversed chars to it(see the StringBuffer Class). After that set the text into the text area. as for the histogram it is not something easy and its quite hard to accomplish so i would advice you to get an open source third party graphing library.

I hope this helps you

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Reply

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




Views: 3023 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC