2014-10-28 81 views
-1

我尋找類似的問題,但不能真正將任何解決方案應用於我的。我測試了一個本應閱讀有關從文件中汽車購買信息的程序代碼,但在我的測試驅動類它不斷給這個錯誤繼承沒有這樣的元素?

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 
+0

你應該真的向我們註明哪一行會引發異常 – Radiodef 2014-10-28 03:05:56

回答

0

你不應該盲目地調用nextToken()方法。最好的辦法是在你知道有下一個令牌可以獲得之後調用它。

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

在上面的代碼,可以大膽調用nextToken()方法,因爲如果記號賦予物品通過hasMoreTokens()測試它只會被調用。

我的第二個建議是,你使用正則表達式來獲得您所需要的(而不是調用nextToken()這麼多次的標記。我建議你找一個正則表達式的教程,並找出實現這個更好的方法。我建議Lars Vogel's Java Regex Tutorial.我還建議使用在線正則表達式測試器,這樣,您可以創建您的表達式並在線評估它們,這對於處理複雜的正則表達式時幫助了我很大的幫助,這是我過去使用的正則表達式在線測試器:http://java-regex-tester.appspot.com/

0

正確輸入。 「月」令牌似乎缺失。

+1

不僅月份令牌丟失,該行的所有額外令牌都將丟失....所以你是l只需輸入3個標記就可以輸入文件中的行。 – 2014-10-28 03:13:20

+0

最後一行必須有X.我會說,你得到的錯誤的指針是在這個文件的輸入行中。 – 2014-10-28 04:12:38