2016-12-30 1 views
1

안녕하세요 여러분 질문 형식 배열을 만들려고 해요하지만 난 텍스트 파일에있는 질문을 계산에 문제가 있다고 생각합니다 텍스트 파일에 다음 줄이 있습니다.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; 
} 
+0

,하지만 당신은 루프 조건에서 그것을 사용하고, 그래서 조건을 평가 한 결과 것입니다 루프 도중 변경되지 않으며, 루프에 들어가기 전에'text '값에 따라 영원히 또는 한 번도 반복되지 않습니다. 스레드 "주요"java.util.NoSuchElementException에서 – SantiBailors

답변

1

호프가 도움이 될 것입니다 ..! 어떤 라인이 사용

대신

while(!text.equals("")) 
{ 
    file.nextLine(); 
} 

while(text.equals(" ")) 
{ 
    file.nextLine(); 
} 

발견 및시켜 try..catch 블록 예에서 코드를 작성 :

스레드 "주요"java.util.NoSuchElementException의 예외를 방지하려면. 그 다음 줄이 있는지 확인하지 않고

private static int countQuestions() throws FileNotFoundException{ 
    int counter = 0; 
    Scanner file = new Scanner (new File("testBank.txt")); 
    while(file.hasNextLine()){ 
     try 
     { 
      // 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++; 
     } 
     catch(NoSuchElementException e) 
     { 
      //Found End of File 
     } 
    } 
    return counter; 
} 

확인 아래 코드에서이 일

public static Question[] readAllQuestions() throws FileNotFoundException 
{ 
    int numberOfQuestions = countQuestions(); 
    Question [] allQuestions = new Question[numberOfQuestions]; 
    Scanner file = new Scanner (new File("testBank.txt")); 
    try 
    {  
     for (int i = 0 ; i < allQuestions.length ; i++) 
     { 
      String text = file.nextLine(); 
      String q = ""; 
      while(text.equals(" ")){ 
       file.nextLine(); 
      } 
      q += text; 
      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; 
     } 
    } 
    catch(NoSuchElementException e) 
    { 
     //Found End of File 
    } 
    return allQuestions; 
} 
+0

같은 문제 예외 : 없음 라인이 Question.countQuestions (Question.java:32) 질문에 \t에서 java.util.Scanner.nextLine (알 수없는 소스) \t에서 \t하는 결과가 없습니다. readAllQuestions (Question.java:44) \t at test.main (test.java:7) – Ammar

+0

허용으로 표시하십시오. 문제가 해결되면 .. !!! – Pradnyarani

+0

그것은 효과가 있었지만 두 번째 방법에서 질문과 대답을위한 변수를 할당하려고 할 때 또 하나의 문제가 발생했습니다 readAllQuestions() – Ammar

0

당신이 file.nextLine을 요구하고있다(). 또한 내가 틀렸다고해도 루프 안에서만 한 번만 호출하고 싶을 수도 있습니다. 코드 아래

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(); // no check if new line exist 
    } 
    file.nextLine(); 
    file.nextLine(); 
    file.nextLine(); 
    file.nextLine(); 
    file.nextLine(); 
    file.nextLine(); 
    file.nextLine(); 
    file.nextLine(); 
    file.nextLine(); 
    file.nextLine(); 

    counter++; 
} 

대신 사용

while(file.hasNextLine()){ 
    // check if line empty 
    String text = file.nextLine(); 
    while(!text.equals(" ")){ 
     counter++; 
    }  
} 
당신이`업데이트 텍스트 '되지 while` 루프`내부 내부
관련 문제