java - NoSuchElementException 读取/扫描输入

标签 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 以及靠近底部的 (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);
  }
} 

} }

最佳答案

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);            
    }         
} 

你的逻辑让你感到困惑。读取.nextLine(); “使扫描仪前进到当前行并返回跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。该位置设置为下一行的开头。”

所以你是说,它有一条线吗?如果是这样,请阅读接下来的 10...好吧...11 行,这不是您想要的。您不知道是否有 11 行超过此点。不知道该文本文件是什么样的,但您需要重组此部分以表示“当它有下一行”或“读取 11 行”

关于java - NoSuchElementException 读取/扫描输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11848654/

相关文章:

java - 如何确保用户输入的是特定字符串?

java - java中二分法的实现

java - 如何在 Java/Scala 中生成 TimeUUID

java - 为什么 httpbuilder 没有给出准确的 JSON 输出?

java - Hibernate session 和并发

java - Eclipse GridData 的垂直滚动条

java - 从数据库读取时出现"Invalid cursor state"错误

java - 实例化多个场景会导致 "java.lang.NullPointerException"

java - android中如何显示日期格式?

java - 使用 Pixelmon mod 1.12.2-6.3.1 在 Eclipse 中运行 Minecraft Forge 服务器/客户端