안녕하세요 여러분 질문 형식 배열을 만들려고 해요하지만 난 텍스트 파일에있는 질문을 계산에 문제가 있다고 생각합니다 텍스트 파일에 다음 줄이 있습니다.Java에서 줄 단위로 파일을 읽으려고 시도
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Question.countQuestions(Question.java:27) at Question.readAllQuestions(Question.java:44) at test.main(test.java:7)
텍스트 파일의 예 :
01 : 여기 enter link description here내 코드있어 빈 줄 다음 다른 라인은 빈 줄 등은 ... 그리고 난 오류가 발생합니다
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Question {
private String q; private String a;
private String b; private String c;
private String d; private String cA;
public Question(String q, String a, String b, String c, String d, String cA) {
this.q = q; this.a = a;
this.b = b; this.c = c;
this.d = d; this.cA = cA;
}
private static int countQuestions() throws FileNotFoundException{
int counter = 0;
Scanner file = new Scanner (new File("testBank.txt"));
while(file.hasNextLine()){
// check if line empty
String text = file.nextLine();
while(!text.equals("")){
file.nextLine();
}
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
counter++;
}
return counter;
}
public static Question[] readAllQuestions() throws FileNotFoundException{
int numberOfQuestions = countQuestions();
Question [] allQuestions = new Question[numberOfQuestions];
Scanner file = new Scanner (new File("testBank.txt"));
for (int i = 0 ; i < allQuestions.length ; i++){
String text = file.nextLine();
String q = "";
while(!text.equals("")){
q += file.nextLine();
}
String a=file.nextLine();
file.nextLine();
String b=file.nextLine();
file.nextLine();
String c=file.nextLine();
file.nextLine();
String d=file.nextLine();
file.nextLine();
String cA=file.nextLine();
file.nextLine();
Question question = new Question(q,a,b,c,d,cA);
allQuestions[i] = question;
}
return allQuestions;
}
,하지만 당신은 루프 조건에서 그것을 사용하고, 그래서 조건을 평가 한 결과 것입니다 루프 도중 변경되지 않으며, 루프에 들어가기 전에'text '값에 따라 영원히 또는 한 번도 반복되지 않습니다. 스레드 "주요"java.util.NoSuchElementException에서 – SantiBailors