gpt4 book ai didi

java - 我的 tokenizer 类中的线程 "main"java.util.NoSuchElementException 中出现异常

转载 作者:太空宇宙 更新时间:2023-11-04 13:41:20 25 4
gpt4 key购买 nike

每当我运行程序时,都会出现以下错误:

Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:349) at runCar.main(runCar.java:40) the assignment says that I need to close the file, but I am unsure on how to do so, and I haven't found any answers online.

这是我当前的代码:

import java.io.IOException;
import java.util.StringTokenizer;
import java.io.*;

public class runCar {

public static void main(String[] args) throws IOException{

StringTokenizer tokenizer;
String line, code, file="car.txt";
double dealerCost, price;
int idNumber, day, year, modelYear;
String makeModel, month, customer;
Date dateArrived, dateSold;
Car[] items = new Car[13];
Car[] placeholder = new Car[1];

FileReader fr = new FileReader(file);
BufferedReader inFile = new BufferedReader(fr);
int i = 0;
line= inFile.readLine();
tokenizer = new StringTokenizer(line," ");
code = tokenizer.nextToken();

while (code.charAt(0)!='X'){
System.out.println();
System.out.println(line);
if (code.equals("C") || code.equals("c")){
dealerCost=Double.parseDouble(tokenizer.nextToken());
idNumber=Integer.parseInt(tokenizer.nextToken());
month=(tokenizer.nextToken());
day=Integer.parseInt(tokenizer.nextToken());
year=Integer.parseInt(tokenizer.nextToken());
modelYear=Integer.parseInt(tokenizer.nextToken());
makeModel=(tokenizer.nextToken());
dateArrived=new Date(day, month, year);
items[i]=new Car(dealerCost, idNumber, modelYear, dateArrived, makeModel);
i++;
}//end "if c"
else if (code.equals("S1") || code.equals("s1"))
{
idNumber=Integer.parseInt(tokenizer.nextToken());
price=Double.parseDouble(tokenizer.nextToken());
month=(tokenizer.nextToken());
day=Integer.parseInt(tokenizer.nextToken());
year=Integer.parseInt(tokenizer.nextToken());
customer=(tokenizer.nextToken());
dateSold=new Date(day, month, year);
for (int j=0; j < items.length; j++)
{ if (idNumber==items[0].getidNumber())
{ placeholder[0]=items[j];
items[j]=new SoldCar (placeholder[0].getdealerCost(), placeholder[0].getidNumber(),
placeholder[0].getmodelYear(), placeholder[0].getdateArrived(),
placeholder[0].getmakeModel(), price, customer, dateSold);
}

}

i++;

}//end "if s1"
else if (code.equals("S2") || code.equals("s2"))
{
dealerCost=Double.parseDouble(tokenizer.nextToken());
idNumber=Integer.parseInt(tokenizer.nextToken());
month=(tokenizer.nextToken());
day=Integer.parseInt(tokenizer.nextToken());
year=Integer.parseInt(tokenizer.nextToken());
modelYear=Integer.parseInt(tokenizer.nextToken());
makeModel=(tokenizer.nextToken());
dateArrived=new Date(day, month, year);
price=Double.parseDouble(tokenizer.nextToken());
month=(tokenizer.nextToken());
day=Integer.parseInt(tokenizer.nextToken());
year=Integer.parseInt(tokenizer.nextToken());
customer=(tokenizer.nextToken());
dateSold=new Date(day, month, year);
items[i]=new SoldCar(dealerCost, idNumber, modelYear, dateArrived, makeModel,
price, customer, dateSold);
i++;

}//end "if s2"
else
System.out.println ("Error, wrong intital code. Use C, S1, or S2.");
line= inFile.readLine();
}//end "while not x"

for (int k=0; k < items.length; k++)
System.out.println(items[k] + "\n");
}//ends main
}//end class

最佳答案

inFile.close();

看来你还不够努力:Do I need to close() both FileReader and BufferedReader?

这是另一个可能有帮助的链接:BufferedReader Documentation

关于java - 我的 tokenizer 类中的线程 "main"java.util.NoSuchElementException 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31232644/

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