在线程 "main" java.util.NoSuchElementException 中出现异常

Got exception in thread "main" java.util.NoSuchElementException

刚开始java编程。我搜索了 Whosebug 并看到了针对此错误的各种解决方案,但其中 none 解决了我程序中的问题。程序在 "ENDDATA" 处停止。我确定这是一个我似乎无法弄清楚的简单修复:

student.dat 文件的内容:

MARY 50 60 70 80
SHELLY 34 56 90 100
JOHN 32 54 66 88
ALFRED 21 100 88 75
ENDDATA

程序输出:

 The name of the student is MARY
 His/her average score is 65
 The name of the student is SHELLY
 His/her average score is 70
 The name of the student is JOHN
 His/her average score is 60
 The name of the student is ALFRED
 His/her average score is 71
 The name of the student is ENDDATA
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at Statistics.main(Statistics.java:32)

我的代码:

     import java.util.*;
        import java.io.*;
        public class Statistics {


        public static void main(String[] args)throws IOException {
        Scanner in = new Scanner (new FileReader("c:\students.dat"));

        String name;
        int nameCount = 0;
        int avg = 0;
        int spanish = 0;
        int math = 0;
        int french = 0;
        int english = 0;
        int highSpanish = 0;
        int highMath = 0;
        int highFrench = 0;
        int highEnglish = 0;
        int highAvg = 0;
        name = in.next();
        while (name!= "ENDDATA") {
            nameCount++;
            System.out.printf (" The name of the student is " + name + "\n");
            name = in.next();
            spanish = Integer.parseInt(name);
            if (spanish > highSpanish) {
            highSpanish = spanish;
            }
            name = in.next();
            math = Integer.parseInt(name);
            if (math > highMath) {
            highMath = math;
            }
            name = in.next();
            french = Integer.parseInt(name);
            if (french > highFrench) {
            highFrench = french;
            }
            name = in.next();
            english = Integer.parseInt(name);
            if (english > highEnglish) {
            highEnglish = english;
            }
            avg = (spanish + math + french + english) /4;
            if (avg > highAvg) {
            highAvg = avg;
            }
            System.out.printf (" His/her average score is " + avg + "\n");
            name = in.next();
            }
            System.out.printf (" The number of students in the class are " +         nameCount);
            System.out.printf (" The highest student average is " + highAvg);
            System.out.printf (" The highest score for spanish is " + highSpanish);
            System.out.printf (" The highest score for math is " + highMath);
            System.out.printf (" The highest score for french is " + highFrench);
            System.out.printf (" The highest score for english is " + highEnglish);
        }
    }

很遗憾您没有包含文件的内容。但我希望这会有所帮助。

看看这个。这就是我通常从文本文件或与此相关的任何文件中读取的方式:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestMain {

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    try {
        String oneLineOfYourData = br.readLine();
        while (oneLineOfYourData != null) {
                 // Now depending on how your data is structured, you may consider PARSING the line

                // Then you can insert the rest of your logic here
              oneLineOfYourData = br.readLine();
        }
    } finally {
        br.close();
    }

}

}

希望这能为您指明正确的方向。

如果文件内容与您在评论中发布的内容相同,则此方法有效。请记住,代码依赖于数据的正确性(如果某些分数不存在等可能会失败)。

Scanner in = new Scanner(new FileReader("someFile.txt"));
    in.useDelimiter(System.getProperty("line.separator"));

    String line;
    String token;
    int nameCount = 0;
    int avg = 0;
    int spanish = 0;
    int math = 0;
    int french = 0;
    int english = 0;
    int highSpanish = 0;
    int highMath = 0;
    int highFrench = 0;
    int highEnglish = 0;
    int highAvg = 0;

    while (in.hasNext()) {
        line = in.next();

        String[] splittedLine = StringUtils.split(line);
        token = splittedLine[0];
        if ("ENDDATA".equals(token)) {
            break;
        }
        nameCount++;
        System.out.printf(" The name of the student is " + token + "\n");
        token = splittedLine[1];
        spanish = Integer.parseInt(token);
        if (spanish > highSpanish) {
            highSpanish = spanish;
        }
        token = splittedLine[2];
        math = Integer.parseInt(token);
        if (math > highMath) {
            highMath = math;
        }
        token = splittedLine[3];
        french = Integer.parseInt(token);
        if (french > highFrench) {
            highFrench = french;
        }
        token = splittedLine[4];
        english = Integer.parseInt(token);
        if (english > highEnglish) {
            highEnglish = english;
        }
        avg = (spanish + math + french + english) / 4;
        if (avg > highAvg) {
            highAvg = avg;
        }
        System.out.printf(" His/her average score is " + avg + "\n");

    }
    System.out.printf(" The number of students in the class are " + nameCount);
    System.out.printf(" The highest student average is " + highAvg);
    System.out.printf(" The highest score for spanish is " + highSpanish);
    System.out.printf(" The highest score for math is " + highMath);
    System.out.printf(" The highest score for french is " + highFrench);
    System.out.printf(" The highest score for english is " + highEnglish);
}

它产生输出:

The name of the student is MARY
His/her average score is 65
The name of the student is SHELLY 
His/her average score is 70
The name of the student is JOHN
His/her average score is 60
The name of the student is ALFRED
His/her average score is 71
The number of students in the class are 4 The highest student average is 71 
The highest score for spanish is 50 The highest score for math is 100 The  
highest score for french is 90 The highest score for english is 100

您程序中的主要问题是您使用 != 运算符比较字符串,这对于字符串类型是不正确的,我们使用 .equals() 来比较您必须更改的字符串:

 while (name!= "ENDDATA") 

以下内容:

 while (!name.equals("ENDDATA")) 

更好的方法是使用 in.hasNext() 检查是否已到达文件末尾,而不是手动检查。

并且 noSuchElementException 由于以下语句而被抛出 in.next() 你指的是扫描仪的下一行,而它没有任何下一行。

注意:使用 in.nextInt() 从您的扫描仪读取整数值(西班牙语、数学...)。

为了更好的方法,您必须像这样更改 while 循环:

 while (in.hasNext()) {
  name = in.next();
  spanish = in.nextInt(); 
  // and so on
 }