Guest User

Untitled

a guest
Apr 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. package p06;
  2.  
  3. import java.util.Scanner;
  4. import java.io.FileNotFoundException;
  5. import java.io.PrintWriter;
  6. import java.io.File;
  7. import javax.swing.JOptionPane;
  8.  
  9. public class P06 {
  10.  
  11. public static String INPUT_FILE_NAME = "javaReservedWords.txt";
  12. public static String TEXT = "ReservedWordsCounted.txt";
  13.  
  14. public static void main(String[] args) {
  15. Scanner fin = null;
  16. Scanner fin2 = null;
  17. PrintWriter fout = null;
  18. int length = 0;
  19. int count2 = 0;
  20. String currentWord, fileName;
  21.  
  22.  
  23. try {
  24. fin = new Scanner(new File(INPUT_FILE_NAME));
  25. } catch (FileNotFoundException e) {
  26.  
  27. System.err.println("No such file or directory " + INPUT_FILE_NAME);
  28. System.exit(1);
  29. }
  30. length = fin.nextInt();
  31. String[] Reservedwords = new String[length];
  32.  
  33. for (int count = 0; count < length; count++) {
  34. Reservedwords[count] = fin.next();
  35. }
  36.  
  37. try {
  38. fout = new PrintWriter(TEXT);
  39. } catch (FileNotFoundException e) {
  40.  
  41. System.err.println("Error opening the file " + TEXT);
  42. System.exit(1);
  43. }
  44.  
  45. do {
  46. fileName = JOptionPane.showInputDialog(null, "Enter the name of a file:", "Input", 2);
  47.  
  48.  
  49. try {
  50. fin2 = new Scanner(new File(fileName));
  51. } catch (FileNotFoundException e) {
  52. System.err.println("Error opening the file " + fileName);
  53. }
  54. } while (fin2 == null);
  55.  
  56. JOptionPane.showMessageDialog(null, "Counting Reserved Words in '" + fileName + "'", "Message", 2);
  57. JOptionPane.showMessageDialog(null, "Writing Count Results to '" + TEXT + "'", "Message", 2);
  58. JOptionPane.showMessageDialog(null, "Done Writing Results.", "Message", 2);
  59.  
  60. int[] Wordscounted = new int[length];
  61.  
  62. while (fin2.hasNext()) {
  63. currentWord = fin2.next();
  64.  
  65. for (int count = 0; count < length; count++) {
  66. if (currentWord.equals(Reservedwords[count])) {
  67. Wordscounted[count]++;
  68. count2++;
  69. }
  70. }
  71. }
  72.  
  73. for (int count = 0; count < length; count++) {
  74. fout.println(Reservedwords[count] + " : " + Wordscounted[count]);
  75. }
  76. fout.println("Total words found: " + length);
  77. fout.println("Total reserved words found: " + count2);
  78. fout.close();
  79. }
  80. }
Add Comment
Please, Sign In to add comment