import java.io.*;
import java.util.*;
class WordCount {
public static void main(String[] args) {
int characterCount = 0;
int wordCount = 0;
int lineCount = 0;
Scanner sc = new Scanner(System.in);
File filename = new File(sc.nextLine());
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
lineCount++;
line = line.trim();
characterCount += line.length();
if (!line.isEmpty()) {
String[] words = line.split("\\s+");
wordCount += words.length;
}
}
System.out.println("The number of characters is " + characterCount);
System.out.println("The number of words is " + wordCount);
System.out.println("The number of lines is " + lineCount);
} catch (FileNotFoundException e) {
System.out.println("The file " + filename + " was not found.");
} catch (IOException e) {
System.out.println("An error occurred while reading the file.");
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: