我正在制作一个程序,该程序可查看文本文件并将其以eclipse格式输出到控制台。文本文件中的其中一行看起来像这样……
A.Matthews 4 7 3 10 14 50
这是程序
import java.io.*; // for File
import java.util.*; // for Scanner
public class MapleLeafLab {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("mapleleafscoring.txt"));
while (input.hasNextLine()) {
String line = input.nextLine();
Scanner lineScan = new Scanner(line);
String name = lineScan.next(); // e.g. "Eric"
String rest = lineScan.next();
int GP = lineScan.nextInt(); // e.g. 456
int G = lineScan.nextInt();
int A = lineScan.nextInt();
int P = lineScan.nextInt();
int S = lineScan.nextInt();
Double SS = lineScan.nextDouble();
System.out.println(name+rest+" "+GP+" "+G+" "+A+" "+P+" "+S+" "+SS);
//System.out.println(name + " (ID#" + id + ") worked " +
// sum + " hours (" + average + " hours/day)");
}
}
}
答案 0 :(得分:1)
这是扫描仪的Javadoc:
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
public double nextDouble()
将输入的下一个标记扫描为双精度...如果下一个标记与上面定义的Float正则表达式匹配 然后令牌将转换为双精度值...
Returns: the double scanned from the input Throws: InputMismatchException - if the next token does not match the Float regular expression, or is out of range NoSuchElementException - if the input is exhausted IllegalStateException - if this scanner is closed
您之所以得到NoSuchElementException
,是因为您试图从7个令牌行中读取8个令牌。
A.Matthews => name
4 => rest
7 => GP
3 => G
10 => A
14 => P
50 => S
SS => NoSuchElementException