나는 모든 수를 1 카운터에 집계하는 방법을 알아 내려고 노력하고 있는데, 이것은 각 라인에서 'a'의 수를 세고 모든 라인을 하나의 라인에 집계하는 방법을 알고 싶습니다.JavaIntro.txt 파일에 "a"문자가 나타나는 횟수를 세는 프로그램을 작성하십시오.
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();
}}}
디버거는 이와 같은 오류를 찾는 데 큰 도움이됩니다. –