Here is the code that u can formulate to find a letter count from a text file.i have pushed an harcoded letter ‘a’ u can change it to dynamic also.
import java.io.*;
import java.util.Scanner;
public class CountTheNumberOfAs {
public static void main(String[] args)throws IOException
{
String fileName = "JavaIntro.txt";
String line = "";
Scanner scanner = new Scanner(new FileReader(fileName));
try {
while ( scanner.hasNextLine() ){
line = scanner.nextLine();
int counter = 0;
for( int i=0; i<line.length(); i++ ) {
if( line.charAt(i) == 'a' ) {
counter++;
}
}
System.out.println(counter);
}
}
finally {
scanner.close();
}}}
4
solved Getting the program to count the total amount of a char input from a user through a text file