gpt4 book ai didi

Java i/o 程序错误 throwFor(未知来源)

转载 作者:行者123 更新时间:2023-12-02 10:42:59 25 4
gpt4 key购买 nike

我正在制作一个程序,用于查看文本文件并将其打印到 Eclipse 中的控制台。文本文件中的其中一行看起来像这样...

A.Matthews 4 7 3 10 14 50

运行程序时,我收到这样的错误.. enter image description here

这就是程序

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)");

}
}
}

最佳答案

这是扫描仪的 Javadoc:

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

public double nextDouble()

Scans the next token of the input as a double... If the next token matches the Float regular expression defined above then the token is converted into a double value...

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 个 token 行中读取 8 个 token 。

A.Matthews => name
4 => rest
7 => GP
3 => G
10 => A
14 => P
50 => S
SS => NoSuchElementException

关于Java i/o 程序错误 throwFor(未知来源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52799448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com