3 Replies - 423 Views - Last Post: 13 October 2012 - 04:55 PM Rate Topic: -----

#1 bluetoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-October 12

Need help with reading from text while loop.

Posted 12 October 2012 - 09:58 PM

Hey everyone, the objective of this project is to add any cars to the array, however, if the car is a duplicate(same id number) then you don't add it. In the text file there are C, S1, and S2 car types. C types are added as Cars. S1 cars make a C car that is already in the array and convert it to a Sold Car. Finally, S2 cars simply add a new Soldcar. My problem is that I can not get this thing to work right. There are on compiler errors, but here are my problems:

1)all 12 cars are never added to the array.(I always have null cars at the end)
2)If i add the (code.equals("S2") part of the code, the initial Car has part of its toString cut out.
-If i comment out that entire section of the code, the program works fine just doesn't add S2 type cars.

Basically I think the problem is with adding S2 cars, but I can't figure out what exactly. The Driver class is the only class I think you will need to look at, but I will add the other classes as well:

Driver class ( I believe the problem is in here).
import java.io.*;
import java.util.*;
public class Driver 
{
public static void main(String[] args) throws IOException
{
        StringTokenizer tokenizer;
        String line, code, file = "car.txt";
        double dCost, price;
        int id, day, year, mYear;
        String mModel, month, customer;
        Date soldDate, buyDate;
        boolean error;
        Car[] items = new Car[13];

        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')
        {
                error = false;
                if(code.equals("C"))
                {
                        dCost = Double.parseDouble(tokenizer.nextToken());
                        id = Integer.parseInt(tokenizer.nextToken());
                        month = tokenizer.nextToken();
                        day = Integer.parseInt(tokenizer.nextToken());        
                        year = Integer.parseInt(tokenizer.nextToken());
                        mYear = Integer.parseInt(tokenizer.nextToken());
                        mModel = tokenizer.nextToken();        
                        buyDate = new Date(day, year, month);
                        Car temp = new Car(dCost, id, buyDate, mYear, mModel);
                        for(int j = 0; j < i; j++)
                            if(id == items[j].getId())
                                error = true;
                            if(i == 0)
                                error = false;
                            if(error == false)
                            {
                                items[i] = temp;
                                i++;
                            }
                        
                        
                        


                }
                else if(code.equals("S1"))
                {
                    id = Integer.parseInt(tokenizer.nextToken());
                    price = Double.parseDouble(tokenizer.nextToken());
                    customer = tokenizer.nextToken();
                    month = tokenizer.nextToken();
                    day = Integer.parseInt(tokenizer.nextToken());
                    year = Integer.parseInt(tokenizer.nextToken());
                    soldDate = new Date(day, year, month);
                    for(int j = 0; j < i; j ++)
                        if(id == items[j].getId())                        
                            items[j] = new SoldCar(items[j], price, customer, soldDate);
                  
                   
                }
                else if(code.equals("S2"))
                {
                    dCost = Double.parseDouble(tokenizer.nextToken());
                    id = Integer.parseInt(tokenizer.nextToken());
                    month = tokenizer.nextToken();
                    day = Integer.parseInt(tokenizer.nextToken());        
                    year = Integer.parseInt(tokenizer.nextToken());
                    buyDate = new Date(day, year, month);
                    mYear = Integer.parseInt(tokenizer.nextToken());
                    mModel = tokenizer.nextToken();     
                    price = Double.parseDouble(tokenizer.nextToken());
                    customer = tokenizer.nextToken();
                    month = tokenizer.nextToken();
                    day = Integer.parseInt(tokenizer.nextToken());        
                    year = Integer.parseInt(tokenizer.nextToken());
                    soldDate = new Date(day, year, month);
                  
                  
                    
                    
                   /* Car temp2 = new SoldCar(dCost, id, buyDate, mYear, mModel, price, customer, soldDate);
                   for(int j = 0; j < i; j++)
                    if(id == items[j].getId())
                        error = true;
                   if(error == false)
                    items[i] = temp2;
                                 */
                       
                    
                    //System.out.println("Hey");
                    
                }
                
                
                line = inFile.readLine();
                tokenizer = new StringTokenizer(line, " ");
                code = tokenizer.nextToken();
               
                
                
                        


        }
        System.out.println(items[0]);
        System.out.println(items[1]);
        System.out.println(items[2]);
        System.out.println(items[3]);
        System.out.println(items[4]);
        System.out.println(items[5]);
        System.out.println(items[6]);
        System.out.println(items[7]);
        System.out.println(items[8]);
        System.out.println(items[9]);
        System.out.println(items[10]);
        System.out.println(items[11]);
}
}





public class Car
 {
    protected double dealerCost;
    protected int idNumber;
    protected Date dateArrived;
    protected int modelYear;
    protected String makeModel;
    
    //Default constructor
    public Car()
    {
        dealerCost = 0;
        idNumber = 0;
        dateArrived = new Date();
        modelYear = 0;
        makeModel = "unknown";
    }
    
    //Paramaterized constructor
    public Car(double dCost, int id, Date dArrive, int mYear, String model)
    {
        dealerCost = dCost;
        idNumber = id;
        dateArrived = dArrive;
        modelYear = mYear;
        makeModel = model;
    }
    
    //toString
    public String toString()
    {
        String str = "Car info:\n\n";
        str += "Car model:\t"  + modelYear + " " + makeModel + "\nDate arrived:\t" + dateArrived + "\nDealer cost:\t" + dealerCost + "\nId number:\t" + idNumber;
        return str;
    }
    
    public int getId()
    {
        return idNumber;        
    }
    
    public boolean equals(Car c)
    {
        if(modelYear == c.modelYear && dateArrived.equals(c.dateArrived) && makeModel.equals(c.makeModel))
            return true;
        else
            return false;
    }
    
 }



 
 public class SoldCar extends Car
 {
    private double price;
    private String customer;
    private Date dateSold;
    
    public SoldCar()
    {
        super();
        price = 0;
        customer = "unknown";
        dateSold = new Date();
        
    }
    
    public SoldCar(Car c, double p, String cus, Date sold)
    {
        super(c.dealerCost, c.idNumber, c.dateArrived, c.modelYear, c.makeModel);
        price = p;
        customer = cus;
        dateSold = sold;
        
    }
    
    public SoldCar(double dCost, int id, Date dArrive, int mYear, String model, double p, String cus, Date sold)
    {
        super(dCost, id, dArrive, mYear, model);
        price = p;
        customer = cus;
        dateSold = sold;
    }
    
    public boolean equals(SoldCar c)
    {
        if((super.equals(c)) && dateSold.equals(c.dateSold))
            return true;
        else
            return false;
    }
    
    public String toString()
    {
        String str = super.toString();
        str += "\nPrice:\t\t" + price + "\nCustomer:\t" + customer + "\nDate Sold:\t" + dateSold;
        return str;
    }
     
     
 }


public class Date
{
    private int day;
    private int year;
    private String month;
    
    //Default constructor
    public Date()
    {
        day = 0;
        year = 0;
        month = "Unknown";
    }
    
    //Paramaterized constructor
    public Date(int xDay, int xYear, String xMonth)
    {
        day = xDay;
        year = xYear;
        month = xMonth;
    }
    
    //Equals
    public boolean equals(Date o)
    {
        if(day == o.day && year == o.year && month.equals(o.month))
            return true;
        else
            return false;
    }
    
    //toString prints out in format 'Date: month/day/year'.
    public String toString()
    {
        
        String str = month + "/" + day + "/" + year;
        return str;
    }
}



And here is the text file car.txt
C 5000.00 1234 January 1 2004 2000 Honda_Accord
S1 1234 6000.00 Roth February 15 2004
S2 8000.00 2222 January 15 2004 1999 Ford_F150 9500.00 Jones January 25 2004
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
S2 12000.00 5555 February 6 2003 1965 Ford_Mustang 15000 Williams September 4 2003
S1 3333 15650 Brown December 25 2003
C 10500.00 4000 July 10 2002 2001 Toyota_Tundra
S2 9500.00 6666 January 21 2004 1999 Ford_F150 9000.00 Smith January 31 2004
C 5000.00 1222 January 1 2004 2000 Honda_Civic
S2 8000.00 2222 January 15 2004 1999 Ford_F150 9500.00 Jones January 25 2004
S2 9900.00 6667 January 21 2004 1999 Ford_F150 9007.00 Byrd January 31 20042004
X




Is This A Good Question/Topic? 0
  • +

Replies To: Need help with reading from text while loop.

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Need help with reading from text while loop.

Posted 12 October 2012 - 11:46 PM

The reason you have null cars at the end, is because your array is longer than the number of cars you have loaded.
Car[] items = new Car[13];

That code is going to allocate space for 13 cars, always. If you are looking for something that will change sizes ArrayList might be what you are looking for. I'm guessing that when you commented out your code, the S2 cars is what your nulls should have been.
Was This Post Helpful? 0
  • +
  • -

#3 bluetoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-October 12

Re: Need help with reading from text while loop.

Posted 13 October 2012 - 07:32 AM

But even when i don't comment out the S2 cars, the S1 cars are never added either, as the C cars they are supposed to replace never switch from cars to sold cars.
Was This Post Helpful? 0
  • +
  • -

#4 bluetoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-October 12

Re: Need help with reading from text while loop.

Posted 13 October 2012 - 04:55 PM

Bump? I am extremely confused.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1