我尋找類似的問題,但不能真正將任何解決方案應用於我的。我測試了一個本應閱讀有關從文件中汽車購買信息的程序代碼,但在我的測試驅動類它不斷給這個錯誤繼承沒有這樣的元素?
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at Project4.TestDrive.main(TestDrive.java:24)
這是我到目前爲止有:
汽車類:
public class Car
{
protected double dealerCost;
protected int idNum, modelYear;
protected Date dateA;
protected String makeModel;
public Car()
{
dealerCost = 0;
idNum = 0;
dateA = new Date();
modelYear = 0;
makeModel = "";
}
public Car(double d, int i, Date da, int mY, String makeM)
{
dealerCost = d;
idNum = i;
dateA = da;
modelYear = mY;
makeModel = makeM;
}
public double getDealerCost()
{ return dealerCost;}
public int getIDNumber()
{ return idNum;}
public Date getDateArrived()
{ return dateA;}
public int getModelYear()
{ return modelYear;}
public String getMakeModel()
{ return makeModel;}
public boolean equals(Car other)
{
return Math.abs(dealerCost-other.dealerCost)<.0001&&
idNum == other.idNum&&dateA.equals(other.dateA)&&
modelYear==other.modelYear&&makeModel.equalsIgnoreCase(
other.makeModel);
}
public String toString()
{
return "Dealer Cost:\t"+dealerCost+"\nId Number:\t"+
idNum+"\nDate Arrived:\t"+dateA+"\nMake:\t"+
makeModel+"\nModel:\t"+modelYear;
}
}
Date類:
import java.io.Serializable;
//Design and code a class called Date that includes two integer instance variables
public class Date
{
private int day, year;
private String month;
public Date()
{
day = 0;
year = 0;
month = "";
}
public Date(String m, int d, int y)
{
day = d;
year = y;
month = m;
}
public int dayIs()
{
return day;
}
public String monthIs()
{
return month;
}
public int yearIs()
{
return year;
}
public boolean equals(Date object)
{
return day==object.day&&year==object.year&&
month.equalsIgnoreCase(object.month);
}
public String toString()
{
return month+", "+day+", "+year;
}
}
SoldCar類:
測試驅動類:
import java.io.*;
import java.util.StringTokenizer;
public class TestDrive
{
public static void main(String[] args) throws IOException
{ StringTokenizer t;
Car[] Info = new Car[13];
int idNum, d, y, mY, q =0;
String make, model, month, customer, l;
Date B, S;
double price, price2;
FileReader fr = new FileReader("cars.txt");
BufferedReader br = new BufferedReader(fr);
String stringRead = br.readLine();
t = new StringTokenizer(stringRead, " ");
l = t.nextToken();
while(l.charAt(0)!='X')
{
if(l.equalsIgnoreCase("c"))
{
price=Double.parseDouble(t.nextToken());
idNum = Integer.parseInt(t.nextToken());
month = t.nextToken();
d = Integer.parseInt(t.nextToken());
y = Integer.parseInt(t.nextToken());
mY = Integer.parseInt(t.nextToken());
model = t.nextToken();
B = new Date(month, d, y);
Info[q] = new Car(price, idNum, B, mY, model);
for(int i = 0; i < q; i++)
{
if(Info[q].getIDNumber()==idNum)
{
System.out.println("Same idNumber as another"+
" car in the array!");
Info[q] = null;
q--;
}
if(Info[q].equals(Info[i])==true)
{
System.out.println("This car showed up more than once: "+
Info[i].getIDNumber());
}
}
}
else if(l.equalsIgnoreCase("S1"))
{
idNum = Integer.parseInt(t.nextToken());
price=Double.parseDouble(t.nextToken());
customer=t.nextToken();
month = t.nextToken();
d = Integer.parseInt(t.nextToken());
y = Integer.parseInt(t.nextToken());
S=new Date(month, d, y);
Info[q]=new SoldCar(idNum, price, customer, S);
q++;
}
else
{
price=Double.parseDouble(t.nextToken());
idNum = Integer.parseInt(t.nextToken());
month = t.nextToken();
d = Integer.parseInt(t.nextToken());
y = Integer.parseInt(t.nextToken());
mY = Integer.parseInt(t.nextToken());
model = t.nextToken();
B = new Date(month, d, y);
price2=Double.parseDouble(t.nextToken());
customer = t.nextToken();
month = t.nextToken();
d = Integer.parseInt(t.nextToken());
y = Integer.parseInt(t.nextToken());
S = new Date(month, d, y);
Info[q]=new SoldCar(price, idNum, B, mY, model,
price2, customer, S);
for(int i = 0; i < q; i++)
{
if(Info[q].getIDNumber()==idNum)
{
System.out.println("Same idNumber as another"+
" car in the array!");
Info[q] = null;
q--;
}
if(Info[q].equals(Info[i])==true)
System.out.println("This car showed up more than once: "+
Info[i].getIDNumber());
}
}
}
for(int i = 0; i < q; i++)
{
System.out.println("The remaining data in the array is: "+
Info[q]);
}}}
下面是它的讀取文件:在您的cars.txt
文件
C 5000.00 1234 January 1 2004 2000 Honda_Accord
C 14000.00 3333 June 6 2003 1999 GMC_Suburban
C 5000.00 1222 January 1 2004 2000 Honda_Civic
C 10000.00 4444 July 10 2002 2001 Toyota_Tundra
S 1234 6000.00 Roth February 15 2004
S 1234 6200.00 Smith January 15 2004
S 3333 15500.00 Jones February 6 2004
S 3333 15650.00 Brown December 25 2003
S 1222 6500.00 Thomas January 21 2004
S 1234 5850.00 Hall January 15 2004
S 4444 11250.00 Baker January 21 2004
你應該真的向我們註明哪一行會引發異常 – Radiodef 2014-10-28 03:05:56