NoSuchElementException读取/扫描输入

时间:2012-08-07 14:55:06

标签: java

这是主要问题:

    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以及我的五个jother错误(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);
  }
} 

} }

3 个答案:

答案 0 :(得分: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行”

答案 1 :(得分:0)

删除for循环可能会解决问题。您只需使用while(hasNextLine() )检查一次,但在for循环中调用read.nextLine() 10次。

for (int i = 0; i <= 10; i++)
         {
          responses = read.nextLine();
         .......
           }

答案 2 :(得分: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);

我&lt; numberOfStudents:生成所需的插入次数

read.hasNextLine():检查控制台是否有输入。如果不是,程序等待输入。

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

从0开始计数 - &gt; 10 = 11名学生