2012-08-07 76 views
2

這裏的主要問題是:NoSuchElementException異常讀/掃描輸入

java.util.NoSuchElementException: No line found 
     at java.util.Scanner.nextLine(Unknown Source) 
     at ExamAnalysis.main(ExamAnalysis.java:21) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
     at java.lang.reflect.Method.invoke(Unknown Source) 
     at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271) 

程序編譯和運行。這只是我要麼接近java.util.NoSuchElementException和我的五個錯誤(接近底部的(answer.charAt(i)== char)。下面是我的程序:

import java.io.*; 
import java.util.Scanner; 

class ExamAnalysis 
{ 
    public static void main(String [] args) throws FileNotFoundException 
    { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Please type the correct answers to the exam questions, one right after the other: "); 
     String answers = keyboard.nextLine(); 
     System.out.println("Where is the file with all the student responses? "); 
     String responses = keyboard.nextLine(); 
     Scanner read = new Scanner(new File(responses)); 


     while (read.hasNextLine()) 
     { 
     for (int i = 0; i <= 10; i++) 
     { 
      responses = read.nextLine(); 
      int p = 1; 
      p += i; 
      System.out.println("Student " + p + " responses: " + responses.substring(0,10)); 
      } 
      System.out.println("Thank you for the data on 9 students. Here's the analysis: "); 
      resultsByStudents(responses, answers); 
      analysis(responses); 
      } 
     } 

     public static void resultsByStudents(String responses, String answers) 
      { 
      System.out.println ("Student #  Correct  Incorrect  Blank"); 
      System.out.println ("~~~~~~~~~  ~~~~~~~  ~~~~~~~~~  ~~~~~"); 
      int student = 0; 
      int correct = 0; 
      int incorrect = 0; 
      int blank = 0; 

     for (int i = 0; i <= 9; i++) 
     { 
      for (int j = 0; j <= responses.length(); j++) 
      { 
      if ((responses.charAt(j)) == answers.charAt(j)) 
      correct++; 
      else if ((responses.charAt(j)) != answers.charAt(j)) 
      incorrect++; 
      else 
      blank++; 
      } 
      System.out.println(student + "  " + correct + "  " + incorrect + "  " + blank); 
      student++; 
     }  
     } 

    public static void analysis(String responses) 
    { 
     System.out.println("QUESTION ANALYSIS (* marks the correct response)"); 
     System.out.println("~~~~~~~~~~~~~~~~~"); 


    //stores the percentage of each choice chosen 
    double A = 0; 
    double B = 0; 
    double C = 0; 
    double D = 0; 
    double E = 0; 
    double X = 0; 
    // tallys every variable chosen per question 

    for (int i = 0; i <= 10; i++) // go through all the questions 
    { 
    for (int j = 0; j <= responses.charAt(i); j++) //go through all the student responses 
    { 
     // variable that are being tallied 
     int chooseA = 0; 
     int chooseB = 0; 
     int chooseC = 0; 
     int chooseD = 0; 
     int chooseE = 0; 
     int chooseBlank = 0; 
    //variables take percentage of choices that have been chosen from each student 
     A = chooseA/9; 
     B = chooseB/9; 
     C = chooseC/9; 
     D = chooseD/9; 
     E = chooseE/9; 
     X = chooseBlank/9; 
     // variables that will print the asterisk with certain character of correct answer 
     String a = "A"; 
     String b = "B"; 
     String c = "C"; 
     String d = "D"; 
     String e = "E"; 
     String blank = "blank"; 


     if (responses.charAt(j) == A) 
     chooseA++; 
     else if (responses.charAt(j) == B) 
     chooseB++; 
     else if (responses.charAt(j) == C) 
     chooseC++; 
     else if (responses.charAt(j) == D) 
     chooseD++; 
     else if (responses.charAt(j) == E) 
     chooseE++; 
     else 
     chooseBlank++; 


    System.out.println("Question #" + i); 
     if (answers.charAt(i) == 'A') a = "A*"; // answers cannot be resolved(I already made it a global variable in my main method.) 
     else if (answers.charAt(i) == 'B') b = "B*";// answers cannot be resolved 
     else if (answers.charAt(i) == 'C') c = "C*";// answers cannot be resolved 
     else if (answers.charAt(i) == 'D') d = "D*";// answers cannot be resolved 
     else if (answers.charAt(i) == 'E') e = "E*";// answers cannot be resolved 
     System.out.println(a + "  " + b + "  " + c + "  " + d + "  " + e + "  " + blank); 
    System.out.println (chooseA + "  " + chooseB + "  " + chooseC + "  " + chooseD + "  " + chooseE + "  " + chooseBlank); 
    System.out.println (A + "  " + B + "  " + C + "  " + D + "  " + E + "  " + X); 
    } 
} 

}}

回答

0

刪除for循環可以解決該問題。您只使用while(hasNextLine())檢查一次,但在for循環中調用read.nextLine() 10次。

for (int i = 0; i <= 10; i++) 
     { 
      responses = read.nextLine(); 
     ....... 
      } 
2
while (read.hasNextLine())   
{   
    for (int i = 0; i <= 10; i++)   
    {  
     responses = read.nextLine(); 
     int p = 1;   
     p += i;   
     System.out.println("Student " + p + " responses: " + responses.substring(0,10));    
    }    
    System.out.println("Thank you for the data on 9 students. Here's the analysis: ");    
    resultsByStudents(responses, answers);    
    analysis(responses);    
    }   
} 

你這裏的邏輯是混亂你。 read.nextLine(); 「使掃描器前進到當前行並返回跳過的輸入,此方法返回當前行的其餘部分,不包括結尾處的任何行分隔符,位置設置爲下一行的開始位置。

所以你說,它有一條線嗎?如果是這樣,請閱讀下一個10 ... ... ... 11行,這不是你想要的。你不知道這一點是否有11條線。不知道這是什麼文本文件的樣子,但你會想這部分重組要麼說,「雖然它有一個下一行」或「閱讀11線」

+0

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextLine() – 2012-08-07 15:13:25

+0

是不是我的掃描儀讀取應該讀取什麼在我的輸入文件? – 2012-08-07 16:26:30

+0

我所做的第一部分是將學生增加1,併爲每個學生分配來自我的輸入文件的回覆每個學生將從我的輸入文件中取出一行,其中有10個不同的答案 – 2012-08-07 16:27:42

0
int i = 0; 
int numberOfStudents = 9; 
while (i < numberOfStudents && read.hasNextLine()){ 
    responses = read.nextLine(); 
    i++; 
    System.out.println("Student " + i + " responses: " + responses.substring(0,10)); 
} 
System.out.println("Thank you for the data on "+ numberOfStudents +" students. Here's the  analysis: "); 
resultsByStudents(responses, answers); 
analysis(responses); 

我< numberOfStudents:品牌所需的插入數量

read.hasNextLine():檢查是否有來自控制檯的輸入。如果不是,程序等待輸入。

for (int i = 0; i <= 10; i++) 

計數從0 - > 10 = 11學生