The following program counts the number of occurrences of a given word in a file, but has many problems. It is written using poor style, and it contains syntax errors and even some logic errors. Identify and provide corrections for these problems so that the program follows the style guideline in this chapter and executes properly.
import java.io.*;
public Class countWord {
static final String
promptl = “Enter the name of the text file:”;
static final String
PROMPT2 = “Enter the word to be counted in the file:”
public static void main(String args) {
// Purpose: To count the number of occurrences of a word in a
// text file. The user provides both the word and the text
// file.
Scanner input = new Scanner(System.out);
String Filename = input.nextLine();
System.out.print(promptl);
Scanner filelnput - null; try {
filelnput = new Scanner(new File(Filename));
} catch (FileNotFoundException e) { e.printStackTrace();
}
// Use anything but a letter as a delimiter filelnput.useDelimiter (“[â-zA-Z] + ”);
String word = input.next();
System.out.print(PROMPT2)
while (fileInput.hasNext()) {
String fileWord = fileinput.next();
System.out.println(fileWord);
int color = 0;
if (Word.equalsIgnoreCase(fileWord)) {
color++;
}
} // end while
filelnput.close();
System.out.println("The word + word + "appeared " + color times in the file” + Filename);
}
} // end countWord
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.