import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String fileName; while (true) { System.out.print("Please enter the file name or type QUIT to exit:\n"); fileName = keyboard.nextLine().trim(); if (fileName.equalsIgnoreCase("QUIT")) { break; } File file = new File(fileName); if (file.exists()) { int wordCount = countWords(file); System.out.println("Total number of words: " + wordCount); } else { System.out.println("File: " + fileName + " does not exist."); } } keyboard.close(); } public static int countWords(File file) { int wordCount = 0; try { Scanner inputFile = new Scanner(file); while (inputFile.hasNext()) { String line = inputFile.nextLine().trim(); if (!line.isEmpty()) { String[] words = line.split("\\s+"); wordCount += words.length; } else { wordCount = 0; } } inputFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } return wordCount; } } Chapter 9. PC #10. Word Counter (page 610) Write a program that asks the user for the name of a file. The program should display the number of words that the file contains. Input Validation: Make sure the file exists, before proceeding. Let the user type quit in order to exit the program. If the file is empty, the word count should be 0. input1.txt this is a test input2.txt this is a test input3.txt Empty input4txt this this is is a a test test Test Case 1 Please enter the file name or type QUIT to exit:\n input1.txtENTER Total number of words: 4\n Test Case 2 Please enter the file name or type QUIT to exit:\n input2.txtENTER Total number of words: 4\n Test Case 3 Please enter the file name or type QUIT to exit:\n input3.txtENTER Total number of words: 0\n Test Case 4 Please enter the file name or type QUIT to exit:\n input4.txtENTER Total number of words: 8\n Test Case 5 Please enter the file name or type QUIT to exit:\n input5.txtENTER File: input5.txt does not exist.\n Please enter the file name again or type QUIT to exit:\n input1.txtENTER Total number of words: 4\n Test Case 6 Please enter the file name or type QUIT to exit:\n qUitENTER Test Case 7 Please enter the file name or type QUIT to exit:\n input5.txtENTER File: input5.txt does not exist.\n Please enter the file name again or type QUIT to exit:\n quItENTER
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String fileName;
while (true) {
System.out.print("Please enter the file name or type QUIT to exit:\n");
fileName = keyboard.nextLine().trim();
if (fileName.equalsIgnoreCase("QUIT")) {
break;
}
File file = new File(fileName);
if (file.exists()) {
int wordCount = countWords(file);
System.out.println("Total number of words: " + wordCount);
} else {
System.out.println("File: " + fileName + " does not exist.");
}
}
keyboard.close();
}
public static int countWords(File file) {
int wordCount = 0;
try {
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
String line = inputFile.nextLine().trim();
if (!line.isEmpty()) {
String[] words = line.split("\\s+");
wordCount += words.length;
}
else {
wordCount = 0;
}
}
inputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return wordCount;
}
}
input1.txt
this is a test
input2.txt
this
is
a
test
input3.txt
Empty
input4txt
this this
is is
a a
test test
Test Case 1
input1.txtENTER
Total number of words: 4\n
Test Case 2
input2.txtENTER
Total number of words: 4\n
Test Case 3
input3.txtENTER
Total number of words: 0\n
Test Case 4
input4.txtENTER
Total number of words: 8\n
Test Case 5
input5.txtENTER
File: input5.txt does not exist.\n
Please enter the file name again or type QUIT to exit:\n
input1.txtENTER
Total number of words: 4\n
Test Case 6
qUitENTER
Test Case 7
input5.txtENTER
File: input5.txt does not exist.\n
Please enter the file name again or type QUIT to exit:\n
quItENTER
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images