나는 "a"라는 문자가 몇개인지를 인쇄하려고합니다. 계속해서 도움을 청합니다. 파일을 읽는 모든 문제를 넘어문자열에서 카운트 문자가 발생했습니다.
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File myfile = chooser.getSelectedFile();
try {
Scanner in = new Scanner(myfile);
String word = in.nextLine();
int counter = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'a') {
counter++;
}
}
System.out.println("# of chars: " + counter);
} catch (IOException e) {
System.err.println("A reading error occured");
}
}
인쇄 '단어', 당신은 무엇을 얻습니까? – arshajii
루프 앞에'word'를 출력하려고 했습니까? –
첫 번째 행만 확인하고 있습니다. 파일의 첫 번째 줄에 'a'가 있습니까? – Adarsh