我的java程序赢了';不要导入文本文件

我的java程序赢了';不要导入文本文件,java,arrays,import,records,Java,Arrays,Import,Records,程序应该从文本文件中导入赛车信息,然后将其与最快时间/赛车一起吐出 我不知道出了什么问题,也不知道如何解决。我是否需要在java.io.file()中有文件的完整地址?我正在偏离教科书告诉我的内容,因此它也可能是错误的。文件已经找到,否则您将得到FileNotFoundException。看起来异常是在n.carNumber=infle.nextInt()上引发的因为读入的数据不是有效的int您正在检查您的扫描仪是否有另一个输入标记,而不是它的类型。此外,您仅在检查了一个令牌的存在之后,就试图取

程序应该从文本文件中导入赛车信息,然后将其与最快时间/赛车一起吐出


我不知道出了什么问题,也不知道如何解决。我是否需要在java.io.file()中有文件的完整地址?我正在偏离教科书告诉我的内容,因此它也可能是错误的。

文件已经找到,否则您将得到
FileNotFoundException
。看起来异常是在
n.carNumber=infle.nextInt()上引发的
因为读入的数据不是有效的
int

您正在检查您的
扫描仪是否有另一个输入标记,而不是它的类型。此外,您仅在检查了一个令牌的存在之后,就试图取出多个令牌:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at AndrewLaramore56.main(AndrewLaramore56.java:40)
while(infle.hasNext()&&i
您需要单独检查每个数据段,以验证其类型是否正确,以及是否还有更多数据需要读取:

while (inFile.hasNext() && i < data.length)  // there exists at least one token
{
    RacecarTimes n = new RacecarTimes();
    n.carNumber = inFile.nextInt();          // what if the next token is not an int?
    n.trackDistance = inFile.nextInt();      // you haven't checked for this token at all
    n.track = inFile.nextLine();             // etc
    n.time = inFile.nextDouble();
while(infle.hasNext()&&i
所以我添加了if语句,这允许它给我输出,但似乎不是所有的数字都是int。是什么让一个数字成为int?Java中的pinkanator95是一个从-2147483648到2147483647的整数,包括在内。如果它在这个范围之外,但仍然在-92233720368547808到9223372036854775之间807您可以使用
long
。如果您提供输入样本,我可以更具体地说明输入类型用于存储。这是我的输入文本文件。因此,您的数据是
number trackname
,但您正在尝试读取
int-int-string-double
。看起来您正在尝试解析
Atlanta-Motor-Speedway
作为一个
双精度
,等等。愚蠢的错误。尽管如此,现在它仍然不会导入所有内容。它似乎正在将其他每一条数据加载到一个新记录中(因此第一条记录将打印车号和轨道名称,第二条记录将具有第一条记录的距离和时间等)。
while (inFile.hasNext() && i < data.length)  // there exists at least one token
{
    RacecarTimes n = new RacecarTimes();
    n.carNumber = inFile.nextInt();          // what if the next token is not an int?
    n.trackDistance = inFile.nextInt();      // you haven't checked for this token at all
    n.track = inFile.nextLine();             // etc
    n.time = inFile.nextDouble();
while (inFile.hasNext() && i < data.length)
{
    RacecarTimes n = new RacecarTimes();
    if (inFile.hasNextInt()) {
        n.carNumber = inFile.nextInt();
    }
    if (inFile.hasNextInt()) {
        n.trackDistance = inFile.nextInt();
    }
    // etc