Final answer:
Here is the corrected version of the program that counts the total lines and words in the input scanner.
Step-by-step explanation:
The program provided has several errors. Here is the corrected version:
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Oops6 {
public static void main(String[] args) throws IOException {
File file = new File("example.txt");
FileReader fr = new FileReader(file);
Scanner in = new Scanner(fr);
countWords(in);
}
// Counts total lines and words in the input scanner.
public static void countWords(Scanner input) {
int lineCount = 0;
int wordCount = 0;
while (input.hasNextLine()) {
String line = input.nextLine(); // read one line
lineCount++;
Scanner lineScanner = new Scanner(line);
while (lineScanner.hasNext()) { // tokens in line
String word = lineScanner.next();
wordCount++;
}
lineScanner.close();
}
System.out.println("Lines: " + lineCount);
System.out.println("Words: " + wordCount);
input.close();
}
}