Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
源未知的java.util.NoSuchElementException_Java_Exception_Nosuchelementexception - Fatal编程技术网

源未知的java.util.NoSuchElementException

源未知的java.util.NoSuchElementException,java,exception,nosuchelementexception,Java,Exception,Nosuchelementexception,我不知道为什么会出现这个错误。当我运行我的程序时,我得到了这个 java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) 我只运行了一台扫描仪,在这个while循环之后它就关闭了。我很好奇,是否有人能给我一个提示,告诉


我不知道为什么会出现这个错误。当我运行我的程序时,我得到了这个

java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
我只运行了一台扫描仪,在这个while循环之后它就关闭了。我很好奇,是否有人能给我一个提示,告诉我该怎么做。下面是一些代码。我还对第27行做了评论,因为我认为这就是错误发生的地方

        try {
        File file = new File("src/text.txt");
        File finalGrades = new File("src/nextText.txt");
        PrintWriter output = new PrintWriter(finalGrades);
        File finalGradesReport = new File("src/nextTextReport.txt");
        PrintWriter output2 = new PrintWriter(finalGradesReport);
        Scanner input = new Scanner(file);

        double totalPointsAvailable = input.nextDouble();
        double homeworkWeight = input.nextDouble() / totalPointsAvailable;
        double projectsWeight = input.nextDouble() / totalPointsAvailable;
        double firstExamWeight = input.nextDouble() / totalPointsAvailable;
        double secondExamWeight = input.nextDouble() / totalPointsAvailable;
        double finalExamWeight = input.nextDouble() / totalPointsAvailable;

        int numA = 0, numB = 0, numC = 0, numD = 0, numF = 0, numStudents = 0;
        double totalPercGrades = 0, averageGrades = 0;

        while (input.hasNext()) {
            double hw = input.nextDouble() * homeworkWeight;
            double pw = input.nextDouble() * projectsWeight; //line 27
            double firstEx = input.nextDouble() * firstExamWeight;
            double secondEx = input.nextDouble() * secondExamWeight;
            double finalEx = input.nextDouble() * finalExamWeight;

发生错误有两个原因

  • 在调用
    nextDouble()
    之前,您没有检查是否有足够的输入

  • hasNext()
    检查下一个令牌,但不会通过调用
    nextDouble()
    递增。您想使用
    hasNextDouble()

  • 您应该在while循环中包装
    nextDouble()
    ,并在每个
    nextDouble()
    之前使用一个计数器或一个条件,以确保文件中有一个双标记,并跟踪您在文件中的位置

    应该使用这样的方法:

    //...below Scanner input = new Scanner(file); 
    int tokenCounter = 0;
    
    //This will set all of your variables on static fields.
    while (input.hasNextDouble()) { /* <-- this wraps your nextDouble() call */
      setVariables(input.nextDouble(), tokenCounter++);
    }
    
    //several lines of code later 
    
    public static void setVariables(double inputValue, tokenCounter){
    
      switch(tokenCounter){
       case 0: totalPointsAvailable = inputValue; break;
       case 1: homeworkWeight = inputValue; break;
       case 2: projectsWeight = inputValue; break;
       //Continue the switch/case based on the expected order of doubles
       //and make your variables static sense they seem changes to your code might
       //cause you to break up your main method and static variables will be easier 
       //to access across different methods.
      } 
    }
    
    /…扫描仪输入下方=新扫描仪(文件);
    int-tokenCounter=0;
    //这将在静态字段上设置所有变量。
    
    while(input.hasNextDouble()){/*将您的nextXXX()代码包装成`while(input.hasNext()){..}`可能重复,因为您的文件中没有足够的元素,所以会出现此错误。可能是重复的,谢谢。我将重新编写代码,看看能否使其正常工作。