Implement a program that counts the number of single-letter words in a text file.
Your program should input the name of a text file, and print the number of
single-letter words in that file. The file name is a string, the length of which is between 1 and 60.
The file contains between 1 and 1,000 lines, each of which is between 1 and 60 characters long.
The characters in the file include letters, spaces, and line breaks. The file does not include digits
or punctuation marks, and it also does not include blank lines. A single-letter word in that file
may be a letter surrounded by spaces; a letter in the beginning of a line followed by a space; a
letter in the end of a line preceded by a space; or a single-letter line. In other words, it is a letter
that does not have another letter next to it
ex:We r aliens
We talk in a strange language
We r friendly
ANSWER: 3
What I have tried:
ackage test;
import java.io.*;
public class Test {
public static void main(String [] args) {
String fileName = "temp.txt";
String line = null;
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
}