Problem

The following program counts the number of occurrences of a given word in a file, but has...

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

Step-by-Step Solution

Request Professional Solution

Request Solution!

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.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 2
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT