继承没有这样的元素?

时间:2014-10-28 02:56:32

标签: java file inheritance nosuchelementexception

我找了一个类似的问题,但无法真正应用我的任何解决方案。我正在测试一个程序的代码,该程序应该从文件中读取有关购买汽车的信息,但在我的TestDrive类中,它一直在给出这个错误

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;
}
}

日期类:

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类:

public class SoldCar extends Car
{
private double price;
private String customer;
private Date dateS;
private int i;
public SoldCar(double a, int b, Date v, int mY, String m, double p, String c, Date ds)
{
    super(a, b, v, mY, m);
    price = p;
    customer = c;
    dateS = ds;
}
public SoldCar(int c, double a, String s, Date ds)
{
    i = c;
    price = a;
    customer = s;
    dateS = ds;
}
public double getPrice()
{   return price;}
public String getCustomer()
{   return customer;}
public Date getDateSold()
{   return dateS;}
public Double calcProfit()
{ return(price-dealerCost);}
public String toString()
{
    return super.toString()+"\nPrice:\t"+price+"\nCustomer:\t"+
            customer+"\nProfit:\t"+calcProfit();
}
public boolean equals(SoldCar object)
{
    return super.equals(object)&&Math.abs(price-object.price)<.0001&&
            customer.equalsIgnoreCase(object.customer)&&dateS.equals(
            object.dateS);
}
}

TestDrive类:

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]);
    }}}

这是他们阅读的文件:

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

2 个答案:

答案 0 :(得分:0)

更正cars.txt文件中的输入。似乎缺少“月份”令牌。

答案 1 :(得分:0)

你不应该盲目地调用nextToken()方法。最好的办法就是在你知道有下一个令牌后再调用它。

    StringTokenizer t = new StringTokenizer("foo boo baz bar", " ");
    while(t.hasMoreTokens())
    {
        String token = t.nextToken();
        // Do something with the token
    }

在上面的代码中,我可以安全地调用nextToken()方法,因为只有在tokenizer对象通过hasMoreTokens()测试时才会调用它。

我的第二个建议是你使用正则表达式获取你需要的标记(而不是多次调用nextToken()。我建议你找一个REGEX教程并找出更好的方法来实现它。我建议{ {3}}我还建议使用在线正则表达式测试程序。这样,您可以构建表达式并在线评估它们。这在使用复杂的正则表达式时帮助了我很多。这是我使用的正则表达式在线测试程序在过去:Lars Vogel's Java Regex Tutorial.