I’m very new to Java and am having a bit of trouble with my first assignment for class.
The goal is to read a text file from the computer (para1.txt) and count how many a’s appear in the file.
The code I currently have is able to calculate the number of a’s per line, but not the number of a’s in the entire file, and I’m not sure how to alter my code to fix that.
Here is what I have:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LetterCounter {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("src/para1.txt"));
while (input.hasNextLine()) {
String answer = input.nextLine();
answer = answer.toLowerCase();
char someChar = 'a';
int count = 0;
for (int i = 0; i < answer.length(); i++) {
if (answer.charAt(i) == someChar) {
count++;
}
}
System.out.println(answer);
System.out.println("a - " + count);
}
}
}