我正在制作一个程序,用于查看文本文件并将其打印到 eclipse 中的控制台。文本文件中的一行看起来像这样......A.马修斯 4 7 3 10 14 50运行程序时,我收到这样的错误..这是程序import java.io.*; // for Fileimport java.util.*; // for Scannerpublic 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)"); }}}
1 回答
慕桂英4014372
TA贡献1871条经验 获得超13个赞
这是 Scanner 的 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
添加回答
举报
0/150
提交
取消