刚开始做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);
}
}
答案 0 :(得分:1)
遗憾的是,你没有包含你文件的内容。但我希望这会有所帮助。
看看这个。这就是我通常从文本文件或任何文件中读取的内容:
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();
}
}
}
我希望这能指出你正确的方向。
答案 1 :(得分:1)
你的程序中的主要问题是你使用!=
运算符来比较字符串,这对于String类型是不正确的,我们使用.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
}
答案 2 :(得分:0)
如果文件内容与您在评论中发布的内容相同,则此方法有效。请记住,代码依赖于数据的正确性(如果某些分数不存在则可能会失败等)。
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