Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package p06;
- import java.util.Scanner;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.io.File;
- import javax.swing.JOptionPane;
- public class P06 {
- public static String INPUT_FILE_NAME = "javaReservedWords.txt";
- public static String TEXT = "ReservedWordsCounted.txt";
- public static void main(String[] args) {
- Scanner fin = null;
- Scanner fin2 = null;
- PrintWriter fout = null;
- int length = 0;
- int count2 = 0;
- String currentWord, fileName;
- try {
- fin = new Scanner(new File(INPUT_FILE_NAME));
- } catch (FileNotFoundException e) {
- System.err.println("No such file or directory " + INPUT_FILE_NAME);
- System.exit(1);
- }
- length = fin.nextInt();
- String[] Reservedwords = new String[length];
- for (int count = 0; count < length; count++) {
- Reservedwords[count] = fin.next();
- }
- try {
- fout = new PrintWriter(TEXT);
- } catch (FileNotFoundException e) {
- System.err.println("Error opening the file " + TEXT);
- System.exit(1);
- }
- do {
- fileName = JOptionPane.showInputDialog(null, "Enter the name of a file:", "Input", 2);
- try {
- fin2 = new Scanner(new File(fileName));
- } catch (FileNotFoundException e) {
- System.err.println("Error opening the file " + fileName);
- }
- } while (fin2 == null);
- JOptionPane.showMessageDialog(null, "Counting Reserved Words in '" + fileName + "'", "Message", 2);
- JOptionPane.showMessageDialog(null, "Writing Count Results to '" + TEXT + "'", "Message", 2);
- JOptionPane.showMessageDialog(null, "Done Writing Results.", "Message", 2);
- int[] Wordscounted = new int[length];
- while (fin2.hasNext()) {
- currentWord = fin2.next();
- for (int count = 0; count < length; count++) {
- if (currentWord.equals(Reservedwords[count])) {
- Wordscounted[count]++;
- count2++;
- }
- }
- }
- for (int count = 0; count < length; count++) {
- fout.println(Reservedwords[count] + " : " + Wordscounted[count]);
- }
- fout.println("Total words found: " + length);
- fout.println("Total reserved words found: " + count2);
- fout.close();
- }
- }
Add Comment
Please, Sign In to add comment