2016-12-30 59 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)

文本文件的例子:

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

內側的內'while'循環你沒有更新的'text',但你是在循環條件使用它,所以評估條件的結果在循環過程中不會改變,這會根據文本在進入循環之前具有的值而永遠循環或不循環。 – SantiBailors

回答

1

希望,這將有助於..!

爲了避免線程 「主要」 java.util.NoSuchElementException異常:沒有行找到使用

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

代替

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

和try..catch塊如編寫代碼。

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

同樣的問題異常線程 「main」 java.util.NoSuchElementException:沒有找到行 \t在java.util.Scanner.nextLine(來源不明) \t在Question.countQuestions(Question.java:32) \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++; 
    }  
}