Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at LineIO.main(LineIO.java:39)
빨간색 선이 없습니다. 코드를 게시하겠습니다 ...
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class LineIO {
public static void main(String[] args)throws FileNotFoundException{
Scanner console = new Scanner(System.in);
System.out.print("Enter Input file name: ");
String inputFileName = console.next();
System.out.print("Output file: ");
String outputFileName = console.next();
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputFileName);
int lineNumber = 1;
int lineNumber2 = 10;
int lineNumber3 = 20;
int lineNumber4 = 30;
int lineNumber5 = 40;
System.out.println("Enter name 5 names:");
String person1 = console.next();
String person2 = console.next();
String person3 = console.next();
String person4 = console.next();
String person5 = console.next();
double sum = 0;
int j = 0;
while (j < 10)
{
String line = in.nextLine();
int num = Integer.parseInt(line);
sum = sum+num;
}
double average = sum / 10;
System.out.println(person1 + " average score is " + average);
System.out.println(person2 + " average score is " + lineNumber2);
System.out.println(person3 + " average score is " + lineNumber3);
System.out.println(person4 + " average score is " + lineNumber4);
System.out.println(person5 + " average score is " + lineNumber5);
in.close();
out.close();
}
}
이 오류가 계속 발생하는 이유는 무엇입니까? 오류 메시지를 이해할 수없고 코드가 구현되지 않는 이유를 이해할 수 없습니다. input.txt 및 output.txt 파일이 설정되어 있습니다.
이 게시물에 대한 빠른 검색이 나왔습니다. java.util.NoSuchElementException : No line found
파일에 충분한 줄이 없어야합니다.
파일에 충분한 줄이 있는지 확인하는 것 외에도 다음 줄에 대한 검사를 추가하여이 오류를 방지하십시오.
while (j < 10)
{
if (in.hasNextLine()){
String line = in.nextLine();
int num = Integer.parseInt(line);
sum += num; //minor change here
}
else{
System.out.println("Not enough lines!");
break;
}
j++; //increment j
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다