4 줄이 있지만 프로그램에서 단 한 줄만 찾습니다. 내 코드에 어떤 문제가 있습니까? 입력 파일의내 코드에서 버그로 인해 회선 계산 오류가 발생합니다. 조언을주십시오
내용 :
To be, or not to be: that is the question.
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles.
내 코드 :
는import java.io.*;
import java.util.*;
public class wordcount{
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.print("What is the name of the file? ");
String file = console.nextLine();
Scanner input = new Scanner(new File(file));
int wordCount = 0;
while(input.hasNext()){
String word = input.next();
wordCount++;
}
int lineCount = 0;
while(input.hasNextLine()){
String line = input.nextLine();
lineCount++;
}
System.out.println("total words = " + wordCount);
System.out.println("total lines = " + lineCount);
}
}
멋진시를 썼습니다. –
프로그램이 올바른 파일을 가져 왔습니까? –
그래, 올바른 파일을 얻는다. 단어 수는 나를 위해 일했습니다. 그러나 줄 수는 4 대신에 어떤 이유로 든 1을 제공합니다. – Kizzo