“线程中出现异常”;“主要”;java.util.NoSuchElementException

“线程中出现异常”;“主要”;java.util.NoSuchElementException,java,nosuchelementexception,Java,Nosuchelementexception,刚开始做java编程。我搜索了stackoverflow,看到了各种解决这个错误的方法,但是没有一个在我的程序中解决这个问题。程序在“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 a

刚开始做java编程。我搜索了stackoverflow,看到了各种解决这个错误的方法,但是没有一个在我的程序中解决这个问题。程序在“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();
    }

}
}


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

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

它产生以下输出:

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")) 
更好的方法是使用.hasNext()中的
检查文件的结尾,而不是手动检查

由于.next()中的以下语句
您引用的是扫描仪的下一行,而它没有任何下一行,因此引发了
noSuchElementException

注意:使用.nextInt()中的
读取扫描仪中的整数值(spannish、math等)

为了获得更好的方法,您必须更改while循环,如下所示:

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

c:\\students.dat文件中的内容是什么?我希望你因为这个内容而得到例外这又是什么?让我们看看你的学生的内容怎么样。dat?不要使用==和!=比较字符串时。使用
equals()
@jmcg以便(name!=“ENDDATA”)不比较字符串。睁开你的眼睛,“伙计”@EddyG我站着纠正。但这与此无关。我只是更新了它,以包含文件的内容和程序提供的整个输出。您的程序给出了以下错误:学生的名字是MARY,线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“80 SHELLY”在java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)在java.lang.Integer.parseInt(Integer.java:580)在java.lang.Integer.parseInt(Integer.java:615)在Statistics2.main(Statistics2.java:55)的过程已完成。然后我的输入文件与你的不同。你能用你文件中的数据更新你的问题吗?我已经编辑过了。它在我问题的开头,写着“student.dat文件的内容是:”后面是数据。进一步澄清:我的文件格式是5行独立的数据:MARY 50 60 70 80,然后是另一行数据,如上面我的数据所示,最后一行是ENDDATAI编辑了我的答案,现在应该可以了。您还可以使用带有多个分隔符的
扫描仪
,而不是使用
StringUtils.split
(来自commons lang)方法(以空格分隔每一行)。我将更改为while(name.equals(“ENDDATA”))。该程序现在输出:班级学生人数为0最高学生平均分为0西班牙语最高分为0数学最高分为0法语最高分为0英语最高分为0过程完成。抱歉,
while(!name.equals(“ENDDATA”))
我认为最好使用
while(in.hasNext())
,因为如果使用`while(name.equals(“ENDDATA”)`将永远不会进入while循环,这就是为什么所有值都是零。它在不使用while(in.hasNext())的情况下工作。但是我根据您的建议对它进行了测试,它给出了以下结果:错误:不兼容的类型:boolean无法转换为字符串name=in.hasNext();不,它不是
name=in.hasNext()!!!您只需在while循环中使用
hasNext()
,检查扫描仪是否未到达文件的末尾,而是在文件中,要获取值,您必须对字符串使用
.next()
,对整数使用
.nextInt()
 while (in.hasNext()) {
  name = in.next();
  spanish = in.nextInt(); 
  // and so on
 }