Results 1 to 13 of 13
  1. #1
    scrap is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default Accessing an arrayList of multiple items per index.

    Hi all,

    I am trying to design a parking lot for a class project. I have to read data in from a CSV file and put it into an arrayList to manipulate using stacks. The idea is to have 6 rows in the parking lot, 5 for parking cars and 1 for an overflow row to move cars in and out of to change positions or remove them completely. Each row can hold 3 cars. I have a car class that defines the car, a parkingLot class to make the arrayList of cars, and a class to read in the CSV file.

    So far, I am able to read in the CSV file and put each item into a single index (instruction (in or out), make, model, year, color, and license plate). Then I take the arrayList and call the addCar method of the parkingLot class to add all of the cars to a second array list with each index containing 1 car and its details.

    So far so good. Now I have access the data in the new arrayList of cars to manipulate it using the stacks to move cars around. The problem I am having at this point is reading the data in the arrayList of cars. If I print out the first arrayList with individual items, I get the individual items in succession. When I try to print the arrayList of cars, I get the a memory location or something not each cars details. I get something like this, ParkingLot@addbf1.

    Here is my code so far. I tried to add comments in different spots to describe what I am doing. Also, there are several blocks commented out of things I was trying. Any help pointing me in the right direction would be appreciated. I know its not the prettiest code, but I'll clean it up after I can get it working.

    Thanks


    /**
    A car with its description.
    */
    public class Car
    {
    /**
    * Constructs a car.
    * @param anInstruction either car in the lot or out of the lot
    * @param aMake the make of the car
    * @param aModel the model of the car
    * @param aYear the year of the car
    * @param aColor the color of the car
    * @param aLicensePlate the license plate number of the car
    */
    public Car(String anInstruction, String aMake, String aModel, String aYear, String aColor, String aLicensePlate)
    {
    instruction = anInstruction;
    make = aMake;
    model = aModel;
    year = aYear;
    color = aColor;
    licensePlate = aLicensePlate;

    }

    /**
    *Gets the instruction for a car.
    *@return the instruction
    */
    public String getInstruction()
    {
    return instruction;
    }


    /**
    *Gets the make of the car.
    *@return the make
    */
    public String getMake()
    {
    return make;
    }

    /**
    *Gets the model of the car.
    *@return the model
    */
    public String getModel()
    {
    return model;
    }

    /**
    *Gets the year of the car.
    *@return the year
    */
    public String getYear()
    {
    return year;
    }

    /**
    *Gets the color of the car.
    *@return the color
    */
    public String getColor()
    {
    return color;
    }

    /**
    *Gets the license plate of the car.
    *@return the license plate
    */
    public String getLicensePlate()
    {
    return licensePlate;
    }


    private String instruction;
    private String make;
    private String model;
    private String year;
    private String color;
    private String licensePlate;



    }
    /**
    *
    */

    /**
    * @author Danny
    *
    */
    import java.util.*;

    public class ParkingLot
    {

    public ParkingLot()
    {
    parkingLot = new ArrayList<Car>(); //arrayList parkingLot consisting of cars
    //Stack stack = new Stack();
    //System.out.println(parkingLot);
    }

    public void addCar(Car c1)
    {
    parkingLot.add(c1);
    //Stack stack = new Stack();
    //stack.push(c1);
    //System.out.println(parkingLot);

    }

    public void getCar(int i)
    {
    //int i = 0;
    parkingLot.get(i);
    //Stack stack = new Stack();
    //stack.push(c1);
    //System.out.println(parkingLot);

    }

    // public void addCarOut(Car c1)
    // {
    // parkingLot.add(c1);
    // //Stack stack = new Stack();
    // //stack.push(c1);
    // //System.out.println(parkingLot);
    //
    // }

    // public void addToStack(Car c1)
    // {
    // Stack stack = new Stack();
    // stack.push(c1);
    // }



    /**
    Finds a bank account with a given number.
    @param accountNumber the number to find
    @return the account with the given number, or null if there
    is no such account
    */
    public Car find(String instruction)
    {
    for (Car c1 : parkingLot)
    {
    if (c1.getInstruction() == instruction) // Found a match
    return c1;
    }
    return null; // No match in the entire array list


    }

    private ArrayList<Car> parkingLot;
    private Stack<Car> stack;

    }
    import java.io.*;
    import java.util.*;


    public class CSV
    {
    public static void main(String[] args)
    {
    ParkingLot lot1 = new ParkingLot();
    try
    {
    //csv file containing data
    String cvsFile = "cars.csv";

    //create BufferedReader to read csv file
    BufferedReader reader = new BufferedReader( new FileReader(cvsFile));
    String strLine = "";
    StringTokenizer st = null;
    int lineNumber = 0;
    int tokenNumber = 0;
    int count = 0;

    ArrayList<String> tAL = new ArrayList<String>(); //the arrayList made after reading the csv file, each element in a separate index

    while( (strLine = reader.readLine()) != null)
    {
    lineNumber++;

    //break comma separated line using ","
    st = new StringTokenizer(strLine, ",");
    System.out.println(strLine); //prints each line of the CSV file
    tAL.add(new String(st.nextToken()));
    while (st.hasMoreTokens())
    {
    //display csv values
    tokenNumber++;
    tAL.add(new String(st.nextToken()));}
    //reset token number

    tokenNumber = 0;
    }

    // iterate array list and adds each car with its details to the parkingLot arrayList at a single index
    for (int i = 0; i < tAL.size(); i++)
    {
    if (tAL.get(i).equals( "I"))
    {
    String item1 = (String) tAL.get(i);
    String item2 = (String) tAL.get(i+1);
    String item3 = (String) tAL.get(i+2);
    String item4 = (String) tAL.get(i+3);
    String item5 = (String) tAL.get(i+4);
    String item6 = (String) tAL.get(i+5);
    lot1.addCar(new Car(item1,item2,item3,item4,item5,item6)); //adds all of the cars with I's to the parkingLot arrayList in order
    System.out.println("Item " + i + " = " + item1 + " " + item2+ " " + item3+ " " + item4+ " " + item5+ " " + item6);
    }
    if (tAL.get(i).equals( "O"))
    {
    System.out.println("***********");
    String item1 = (String) tAL.get(i);
    String item2 = (String) tAL.get(i+1);
    System.out.println("Item " + i + " = " + item1 + " " + item2);
    System.out.println("***********");
    lot1.addCar(new Car(item1,item2,null,null,null,null)); //adds all of the cars with O's to the parkingLot arrayList in order

    }
    }

    // String instruction = "I";
    // Car c1 = lot1.find(instruction);
    //System.out.println(new Car(String,String,String,String,String,String)); // need to be able to access the parkingLot arrayList to put items into stacks. Try cloning....
    System.out.println(lot1); // prints ParkingLot@a46701
    //System.out.println(ParkingLot.lot1());
    lot1.getCar(count); //is this doing anything?
    System.out.println(tAL.size()); //156 items in the CSV file
    System.out.println(tAL); //prints the array containing individual items in their own index

    }


    catch(Exception e)
    {
    System.out.println("Exception while reading csv file: " + e);
    }

    }
    }

    The contents of the cars.csv file...
    I,Hyundai,Sonata,2007,Gray,JKL456
    I, Toyota, Prius,2008, Blue,ABC123
    I, Smart, Passion,2007, Green,Go Green
    I, Nissan, Altima,2008, Red,QWE098
    O,Go Green,,,,
    I,Ford, Escape,2008, Silver,MY987
    I,Kia,Sportage,2005, Blue,A2
    I,Chevrolet,Cobalt,2007, Green,V324
    I,Toyota,Prius,2007, Red,Green2
    I,Toyota,Sierra,2005, Blue,BigSur
    I,Kia,Rio,2003, Green,Korean
    I,Proton,Wira,2000,White,Malay1
    I,Dodge,Caravan,1985, Silver,Grand
    I,Toyota,Prius,2008, Blue,Greener
    O,A2,,,,
    I,Honda,Civic,2001, Green,AB46
    I,Kia,Sportage,2003, Green,PRT564
    I,Toyota,Camry,2002, Red,KL098
    I,Mercedes,425,2000,White,UK212
    I,Kia,Optima,2005,Black,KOR942
    O,ABC123,,,,
    O, Green,,,,
    I, Toyota,Highlander,2001, Green,JAP456
    I,Kia,Rio,2005, Red,Bravo
    I, Toyota,Camry,2001, Silver,CAM391
    O, AB46,,,,
    I,Lada,1700i,1990,Black,VODKA
    O,WHOAMI,,,,
    I,Saturn,SL 2,1996,Red,OLD-1
    O, Bravo,,,,
    O, UK212,,,,
    O,QWE098,,,,

  2. #2
    scrap is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    Here is the output I get from the csv main class....

    I,Hyundai,Sonata,2007,Gray,JKL456
    I,Toyota,Prius,2008,Blue,ABC123
    I,Smart,Passion,2007,Green,Go Green
    I, Nissan, Altima,2008, Red,QWE098
    O,Go Green,,,,
    I,Ford, Escape,2008, Silver,MY987
    I,Kia,Sportage,2005, Blue,A2
    I,Chevrolet,Cobalt,2007, Green,V324
    I,Toyota,Prius,2007, Red,Green2
    I,Toyota,Sierra,2005, Blue,BigSur
    I,Kia,Rio,2003, Green,Korean
    I,Proton,Wira,2000,White,Malay1
    I,Dodge,Caravan,1985, Silver,Grand
    I,Toyota,Prius,2008, Blue,Greener
    O,A2,,,,
    I,Honda,Civic,2001, Green,AB46
    I,Kia,Sportage,2003, Green,PRT564
    I,Toyota,Camry,2002, Red,KL098
    I,Mercedes,425,2000,White,UK212
    I,Kia,Optima,2005,Black,KOR942
    O,ABC123,,,,
    O, Green,,,,
    I, Toyota,Highlander,2001, Green,JAP456
    I,Kia,Rio,2005, Red,Bravo
    I, Toyota,Camry,2001, Silver,CAM391
    O, AB46,,,,
    I,Lada,1700i,1990,Black,VODKA
    O,WHOAMI,,,,
    I,Saturn,SL 2,1996,Red,OLD-1
    O, Bravo,,,,
    O, UK212,,,,
    O,QWE098,,,,
    Item 0 = I Hyundai Sonata 2007 Gray JKL456
    Item 6 = I Toyota Prius 2008 Blue ABC123
    Item 12 = I Smart Passion 2007 Green Go Green
    Item 18 = I Nissan Altima 2008 Red QWE098
    ***********
    Item 24 = O Go Green
    ***********
    Item 26 = I Ford Escape 2008 Silver MY987
    Item 32 = I Kia Sportage 2005 Blue A2
    Item 38 = I Chevrolet Cobalt 2007 Green V324
    Item 44 = I Toyota Prius 2007 Red Green2
    Item 50 = I Toyota Sierra 2005 Blue BigSur
    Item 56 = I Kia Rio 2003 Green Korean
    Item 62 = I Proton Wira 2000 White Malay1
    Item 68 = I Dodge Caravan 1985 Silver Grand
    Item 74 = I Toyota Prius 2008 Blue Greener
    ***********
    Item 80 = O A2
    ***********
    Item 82 = I Honda Civic 2001 Green AB46
    Item 88 = I Kia Sportage 2003 Green PRT564
    Item 94 = I Toyota Camry 2002 Red KL098
    Item 100 = I Mercedes 425 2000 White UK212
    Item 106 = I Kia Optima 2005 Black KOR942
    ***********
    Item 112 = O ABC123
    ***********
    ***********
    Item 114 = O Green
    ***********
    Item 116 = I Toyota Highlander 2001 Green JAP456
    Item 122 = I Kia Rio 2005 Red Bravo
    Item 128 = I Toyota Camry 2001 Silver CAM391
    ***********
    Item 134 = O AB46
    ***********
    Item 136 = I Lada 1700i 1990 Black VODKA
    ***********
    Item 142 = O WHOAMI
    ***********
    Item 144 = I Saturn SL 2 1996 Red OLD-1
    ***********
    Item 150 = O Bravo
    ***********
    ***********
    Item 152 = O UK212
    ***********
    ***********
    Item 154 = O QWE098
    ***********
    ParkingLot@addbf1
    156
    [I, Hyundai, Sonata, 2007, Gray, JKL456, I, Toyota, Prius, 2008, Blue, ABC123, I, Smart, Passion, 2007, Green, Go Green, I, Nissan, Altima, 2008, Red, QWE098, O, Go Green, I, Ford, Escape, 2008, Silver, MY987, I, Kia, Sportage, 2005, Blue, A2, I, Chevrolet, Cobalt, 2007, Green, V324, I, Toyota, Prius, 2007, Red, Green2, I, Toyota, Sierra, 2005, Blue, BigSur, I, Kia, Rio, 2003, Green, Korean, I, Proton, Wira, 2000, White, Malay1, I, Dodge, Caravan, 1985, Silver, Grand, I, Toyota, Prius, 2008, Blue, Greener, O, A2, I, Honda, Civic, 2001, Green, AB46, I, Kia, Sportage, 2003, Green, PRT564, I, Toyota, Camry, 2002, Red, KL098, I, Mercedes, 425, 2000, White, UK212, I, Kia, Optima, 2005, Black, KOR942, O, ABC123, O, Green, I, Toyota, Highlander, 2001, Green, JAP456, I, Kia, Rio, 2005, Red, Bravo, I, Toyota, Camry, 2001, Silver, CAM391, O, AB46, I, Lada, 1700i, 1990, Black, VODKA, O, WHOAMI, I, Saturn, SL 2, 1996, Red, OLD-1, O, Bravo, O, UK212, O, QWE098]

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    when you try to do a System.out.println(anObjectInstance), it will internally attempt to call the "toString()" method of that anObjectInstance.

    the built-in List class tries to print nicely the stuff inside of it.

    but by default, when you make your own object, unless you invent what the toString() should display, it usually does that ObjectClass@1112231 looking thing.

    What you can do in your Parking lot class, is add a toString() method that will override the built in (and useless) default one.

    Java Code:
    /**
    * @author Danny
    *
    */
    import java.util.*;
    
    public class ParkingLot
    {
    
    // other things you already have her.
    
    @Override
      public String toString() {
        StringBuilder b = new StringBuilder();
        b.append("Parking Lot");
        b.append("  has ").append(parkingLot.size()).append(" cars.");
        // what ever else you want to have the thing print here.
        return b.toString();
      }
    Note you don't have to be all fancy and use the stringbuilder thing, just working with a string is good too.

    Java Code:
     public String toString() {
       return "A parking lot with " + parkingLot.size() + " cars.";
      }

  4. #4
    scrap is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanks Travis,

    I see how you did that and it works for telling me how many cars I have. How do I access and print details of an individual car. I get errors when I try to do some thing like;

    System.out.println(lot1(1));
    I also want to be able to get the instruction by itself for each car to know how to handle it in a stack to insert or remove it from a row to another. I can use an if statement to do that, but how can I traverse the arrayList to find the next instruction and then do what I want with it. Should I be doing this in the CSV class or in the ParkingLot class?

    I appreciate your help. I have been working on this for days and I just keep getting stuck. Once I figure out how to access the arrayList data, I think it would be simple to add the logic for moving the cars...



    Quote Originally Posted by travishein View Post
    when you try to do a System.out.println(anObjectInstance), it will internally attempt to call the "toString()" method of that anObjectInstance.

    the built-in List class tries to print nicely the stuff inside of it.

    but by default, when you make your own object, unless you invent what the toString() should display, it usually does that ObjectClass@1112231 looking thing.

    What you can do in your Parking lot class, is add a toString() method that will override the built in (and useless) default one.

    Java Code:
    /**
    * @author Danny
    *
    */
    import java.util.*;
    
    public class ParkingLot
    {
    
    // other things you already have her.
    
    @Override
      public String toString() {
        StringBuilder b = new StringBuilder();
        b.append("Parking Lot");
        b.append("  has ").append(parkingLot.size()).append(" cars.");
        // what ever else you want to have the thing print here.
        return b.toString();
      }
    Note you don't have to be all fancy and use the stringbuilder thing, just working with a string is good too.

    Java Code:
     public String toString() {
       return "A parking lot with " + parkingLot.size() + " cars.";
      }

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by scrap View Post
    Java Code:
    System.out.println(lot1(1));
    If 'lot1' isn't a method name that code starts to make your compiler protest.

    kind regards,

    Jos

  6. #6
    scrap is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanks Jos,

    I did the following, but I get errors...

    To the parkingLot class I added...
    public String getCar(int i)
    {
    parkingLot.get(i);
    return getCar(i);
    }
    and to the CSV class I added....
    lot1.getCar(0);
    but I get overflow errors...

    Thanks


    Quote Originally Posted by JosAH View Post
    If 'lot1' isn't a method name that code starts to make your compiler protest.

    kind regards,

    Jos

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Well if getCar calls getCar then calling getCar will call getCar which of course calls getCar ...

    The overflow errors are to be expected then, no?

  8. #8
    scrap is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    I understand what you are saying, but how can I just get the details of a car?

    Quote Originally Posted by r035198x View Post
    Well if getCar calls getCar then calling getCar will call getCar which of course calls getCar ...

    The overflow errors are to be expected then, no?

  9. #9
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    well, since parkinglot here is a list of Car objects, and you wanted to return a string details,
    you could also implement a toString() in the Car class

    Java Code:
      public String toString() {
       return color + " " + year + " " + make + "  " + model + ", license " + licensePlate;
      }
    and then the getCar() in parking lot would be

    Java Code:
    public String getCar(int i)
    {
     Car aCar = parkingLot.get(i);
    return aCar.toString();
    }

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why not make your getCar method return the Car that is at the specified index rather than a String. That way the method is more reusable and does exactly what it says (cohesive).

  11. #11
    scrap is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    Okay, so far so good. Thanks for the help everyone. Now I need to find the instruction for each car to know what to do with them. I am thinking, correct me if I am wrong, that I need to have a find method in the parkingLot class (one is there, but I am not sure if it is correct) to find the instruction and call it in the CVS class. Once I can do that then I need to take that car and put it into a stack in the ParkingLot until the stack is full with 3 cars then start the next stack and so on. When I come to an O instruction, I have to remove cars from their row and put them into the overflow row to access the car I want to remove. Does this sound like I am heading in the right direction?

    Car class
    /**
    A car with its description.
    */
    public class Car
    {
    /**
    * Constructs a car.
    * @param anInstruction either car in the lot or out of the lot
    * @param aMake the make of the car
    * @param aModel the model of the car
    * @param aYear the year of the car
    * @param aColor the color of the car
    * @param aLicensePlate the license plate number of the car
    */
    public Car(String anInstruction, String aMake, String aModel, String aYear, String aColor, String aLicensePlate)
    {
    instruction = anInstruction;
    make = aMake;
    model = aModel;
    year = aYear;
    color = aColor;
    licensePlate = aLicensePlate;

    }

    /**
    *Gets the instruction for a car.
    *@return the instruction
    */
    public String getInstruction()
    {
    return instruction;
    }


    /**
    *Gets the make of the car.
    *@return the make
    */
    public String getMake()
    {
    return make;
    }

    /**
    *Gets the model of the car.
    *@return the model
    */
    public String getModel()
    {
    return model;
    }

    /**
    *Gets the year of the car.
    *@return the year
    */
    public String getYear()
    {
    return year;
    }

    /**
    *Gets the color of the car.
    *@return the color
    */
    public String getColor()
    {
    return color;
    }

    /**
    *Gets the license plate of the car.
    *@return the license plate
    */
    public String getLicensePlate()
    {
    return licensePlate;
    }

    public String toString()
    {
    return instruction + ", " + make + ", " + model + ", " + year + ", " + color + ", " + licensePlate;
    }


    private String instruction;
    private String make;
    private String model;
    private String year;
    private String color;
    private String licensePlate;



    }
    ParkingLot class
    /**
    *
    */

    /**
    * @author Danny
    *
    */
    import java.util.*;

    public class ParkingLot
    {

    public ParkingLot()
    {
    parkingLot = new ArrayList<Car>(); //arrayList parkingLot consisting of cars
    }

    public void addCar(Car c1)
    {
    parkingLot.add(c1);
    }

    public int getSize()
    {
    int s = parkingLot.size();
    return s;
    }

    public String getCar(int i)
    {
    Car aCar = parkingLot.get(i);
    return aCar.toString();
    }


    public String toString()
    {
    return "A parking lot with " + parkingLot.size() + " cars. ";
    }

    /**
    Finds a bank account with a given number.
    @param accountNumber the number to find
    @return the account with the given number, or null if there
    is no such account
    */
    public Car find(String instruction)
    {
    for (Car c1 : parkingLot)
    {
    if (c1.getInstruction() == instruction) // Found a match
    return c1;
    }
    return null; // No match in the entire array list


    }

    private ArrayList<Car> parkingLot;
    private Stack<Car> stack;


    }
    CSV Class
    import java.io.*;
    import java.util.*;


    public class CSV
    {
    public static void main(String[] args)
    {
    ParkingLot lot1 = new ParkingLot();
    try
    {
    //csv file containing data
    String cvsFile = "cars.csv";

    //create BufferedReader to read csv file
    BufferedReader reader = new BufferedReader( new FileReader(cvsFile));
    String strLine = "";
    StringTokenizer st = null;
    int lineNumber = 0;
    int tokenNumber = 0;
    int count = 0;

    ArrayList<String> tAL = new ArrayList<String>(); //the arrayList made after reading the csv file, each element in a separate index
    //ArrayList<String> lot1 = new ArrayList<String>();
    while( (strLine = reader.readLine()) != null)
    {
    lineNumber++;

    //break comma separated line using ","
    st = new StringTokenizer(strLine, ",");
    System.out.println(strLine); //prints each line of the CSV file
    tAL.add(new String(st.nextToken()));
    while (st.hasMoreTokens())
    {
    //display csv values
    tokenNumber++;
    tAL.add(new String(st.nextToken()));}
    //reset token number

    tokenNumber = 0;
    }

    // iterate array list and adds each car with its details to the parkingLot arrayList at a single index
    for (int i = 0; i < tAL.size(); i++)
    {
    if (tAL.get(i).equals( "I"))
    {
    String item1 = (String) tAL.get(i);
    String item2 = (String) tAL.get(i+1);
    String item3 = (String) tAL.get(i+2);
    String item4 = (String) tAL.get(i+3);
    String item5 = (String) tAL.get(i+4);
    String item6 = (String) tAL.get(i+5);
    lot1.addCar(new Car(item1,item2,item3,item4,item5,item6)); //adds all of the cars with I's to the parkingLot arrayList in order
    // lot1.add(new String(item1,item2,item3,item4,item5,item6));
    System.out.println("Item " + i + " = " + item1 + " " + item2+ " " + item3+ " " + item4+ " " + item5+ " " + item6);
    }
    if (tAL.get(i).equals( "O"))
    {
    System.out.println("***********");
    String item1 = (String) tAL.get(i);
    String item2 = (String) tAL.get(i+1);
    System.out.println("Item " + i + " = " + item1 + " " + item2);
    System.out.println("***********");
    lot1.addCar(new Car(item1,item2,null,null,null,null)); //adds all of the cars with O's to the parkingLot arrayList in order

    }
    }

    System.out.println(lot1); // prints the number of cars in the parking lot...32
    System.out.println(tAL.size()); //156 items in the CSV file
    System.out.println(tAL); //prints the array containing individual items in their own index

    System.out.println(lot1.getSize());
    int i=0;
    while (i < lot1.getSize())
    {
    System.out.println(lot1.getCar(i));
    i++;
    }



    }

    catch(Exception e)
    {
    System.out.println("Exception while reading csv file: " + e);
    }

    }
    }

    CSV output
    I,Hyundai,Sonata,2007,Gray,JKL456
    I,Toyota,Prius,2008,Blue,ABC123
    I,Smart,Passion,2007,Green,Go Green
    I, Nissan, Altima,2008, Red,QWE098
    O,Go Green,,,,
    I,Ford, Escape,2008, Silver,MY987
    I,Kia,Sportage,2005, Blue,A2
    I,Chevrolet,Cobalt,2007, Green,V324
    I,Toyota,Prius,2007, Red,Green2
    I,Toyota,Sierra,2005, Blue,BigSur
    I,Kia,Rio,2003, Green,Korean
    I,Proton,Wira,2000,White,Malay1
    I,Dodge,Caravan,1985, Silver,Grand
    I,Toyota,Prius,2008, Blue,Greener
    O,A2,,,,
    I,Honda,Civic,2001, Green,AB46
    I,Kia,Sportage,2003, Green,PRT564
    I,Toyota,Camry,2002, Red,KL098
    I,Mercedes,425,2000,White,UK212
    I,Kia,Optima,2005,Black,KOR942
    O,ABC123,,,,
    O, Green,,,,
    I, Toyota,Highlander,2001, Green,JAP456
    I,Kia,Rio,2005, Red,Bravo
    I, Toyota,Camry,2001, Silver,CAM391
    O, AB46,,,,
    I,Lada,1700i,1990,Black,VODKA
    O,WHOAMI,,,,
    I,Saturn,SL 2,1996,Red,OLD-1
    O, Bravo,,,,
    O, UK212,,,,
    O,QWE098,,,,
    Item 0 = I Hyundai Sonata 2007 Gray JKL456
    Item 6 = I Toyota Prius 2008 Blue ABC123
    Item 12 = I Smart Passion 2007 Green Go Green
    Item 18 = I Nissan Altima 2008 Red QWE098
    ***********
    Item 24 = O Go Green
    ***********
    Item 26 = I Ford Escape 2008 Silver MY987
    Item 32 = I Kia Sportage 2005 Blue A2
    Item 38 = I Chevrolet Cobalt 2007 Green V324
    Item 44 = I Toyota Prius 2007 Red Green2
    Item 50 = I Toyota Sierra 2005 Blue BigSur
    Item 56 = I Kia Rio 2003 Green Korean
    Item 62 = I Proton Wira 2000 White Malay1
    Item 68 = I Dodge Caravan 1985 Silver Grand
    Item 74 = I Toyota Prius 2008 Blue Greener
    ***********
    Item 80 = O A2
    ***********
    Item 82 = I Honda Civic 2001 Green AB46
    Item 88 = I Kia Sportage 2003 Green PRT564
    Item 94 = I Toyota Camry 2002 Red KL098
    Item 100 = I Mercedes 425 2000 White UK212
    Item 106 = I Kia Optima 2005 Black KOR942
    ***********
    Item 112 = O ABC123
    ***********
    ***********
    Item 114 = O Green
    ***********
    Item 116 = I Toyota Highlander 2001 Green JAP456
    Item 122 = I Kia Rio 2005 Red Bravo
    Item 128 = I Toyota Camry 2001 Silver CAM391
    ***********
    Item 134 = O AB46
    ***********
    Item 136 = I Lada 1700i 1990 Black VODKA
    ***********
    Item 142 = O WHOAMI
    ***********
    Item 144 = I Saturn SL 2 1996 Red OLD-1
    ***********
    Item 150 = O Bravo
    ***********
    ***********
    Item 152 = O UK212
    ***********
    ***********
    Item 154 = O QWE098
    ***********
    A parking lot with 32 cars.
    156
    [I, Hyundai, Sonata, 2007, Gray, JKL456, I, Toyota, Prius, 2008, Blue, ABC123, I, Smart, Passion, 2007, Green, Go Green, I, Nissan, Altima, 2008, Red, QWE098, O, Go Green, I, Ford, Escape, 2008, Silver, MY987, I, Kia, Sportage, 2005, Blue, A2, I, Chevrolet, Cobalt, 2007, Green, V324, I, Toyota, Prius, 2007, Red, Green2, I, Toyota, Sierra, 2005, Blue, BigSur, I, Kia, Rio, 2003, Green, Korean, I, Proton, Wira, 2000, White, Malay1, I, Dodge, Caravan, 1985, Silver, Grand, I, Toyota, Prius, 2008, Blue, Greener, O, A2, I, Honda, Civic, 2001, Green, AB46, I, Kia, Sportage, 2003, Green, PRT564, I, Toyota, Camry, 2002, Red, KL098, I, Mercedes, 425, 2000, White, UK212, I, Kia, Optima, 2005, Black, KOR942, O, ABC123, O, Green, I, Toyota, Highlander, 2001, Green, JAP456, I, Kia, Rio, 2005, Red, Bravo, I, Toyota, Camry, 2001, Silver, CAM391, O, AB46, I, Lada, 1700i, 1990, Black, VODKA, O, WHOAMI, I, Saturn, SL 2, 1996, Red, OLD-1, O, Bravo, O, UK212, O, QWE098]
    32
    I, Hyundai, Sonata, 2007, Gray, JKL456
    I, Toyota, Prius, 2008, Blue, ABC123
    I, Smart, Passion, 2007, Green, Go Green
    I, Nissan, Altima, 2008, Red, QWE098
    O, Go Green, null, null, null, null
    I, Ford, Escape, 2008, Silver, MY987
    I, Kia, Sportage, 2005, Blue, A2
    I, Chevrolet, Cobalt, 2007, Green, V324
    I, Toyota, Prius, 2007, Red, Green2
    I, Toyota, Sierra, 2005, Blue, BigSur
    I, Kia, Rio, 2003, Green, Korean
    I, Proton, Wira, 2000, White, Malay1
    I, Dodge, Caravan, 1985, Silver, Grand
    I, Toyota, Prius, 2008, Blue, Greener
    O, A2, null, null, null, null
    I, Honda, Civic, 2001, Green, AB46
    I, Kia, Sportage, 2003, Green, PRT564
    I, Toyota, Camry, 2002, Red, KL098
    I, Mercedes, 425, 2000, White, UK212
    I, Kia, Optima, 2005, Black, KOR942
    O, ABC123, null, null, null, null
    O, Green, null, null, null, null
    I, Toyota, Highlander, 2001, Green, JAP456
    I, Kia, Rio, 2005, Red, Bravo
    I, Toyota, Camry, 2001, Silver, CAM391
    O, AB46, null, null, null, null
    I, Lada, 1700i, 1990, Black, VODKA
    O, WHOAMI, null, null, null, null
    I, Saturn, SL 2, 1996, Red, OLD-1
    O, Bravo, null, null, null, null
    O, UK212, null, null, null, null
    O, QWE098, null, null, null, null

  12. #12
    scrap is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    I have the following method in my parking lot class...

    public Car find(String licensePlate)
    {
    for (Car c1 : parkingLot)
    {
    if (c1.getLicensePlate() == licensePlate) // Found a match
    System.out.println("found match");
    return c1;
    }

    return null; // No match in the entire array list
    and in the CSV main class, I have...
    String licensePlate = "OLD-1";
    Car c1 = lot1.find(licensePlate);
    if (c1 == null)
    System.out.println("No matching plate");
    else
    System.out.println("The plate expected is OLD-1." + " It returns: " + c1.getLicensePlate());
    output...
    The plate expected is OLD-1. It returns: JKL456
    I'm not sure what else to do her. I was trying to find the first instruction I or O, but I couldn't get it to work. So I decided to try with the license plates since they are all unique. When I run the program, I get the first car's license plate, not the one I am trying to find. Suggestions?

    Thanks

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Don't use == for comparing strings.

    Use the equals method instead.

Similar Threads

  1. Null Pointer Exception when adding items to ArrayList
    By ShadowCopy in forum New To Java
    Replies: 3
    Last Post: 08-27-2009, 09:23 AM
  2. jlist items
    By bullstreetboy in forum New To Java
    Replies: 6
    Last Post: 04-12-2009, 05:30 PM
  3. different sort of items
    By jrleonard in forum SWT / JFace
    Replies: 0
    Last Post: 12-23-2008, 02:41 PM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. Accessing index.dat files
    By vissu007 in forum Networking
    Replies: 1
    Last Post: 07-01-2007, 04:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •