Link to home
Start Free TrialLog in
Avatar of Enview
Enview

asked on

ArrayList: trouble with the cast operator using the get method

I'm using a sample code example to ask my question. I'm building an ArrayList of Cars and each car has a name(string) and a number(int). I having trouble with the cast operator using the get method. Errors of type; cannot convert primitive int, or I see a listing of memory referrences to each car in the list.  
public class Cars
{
  ArrayList<Car> CarAttributes = new ArrayList<Car>();
  public static void main(String[] args)
  {
    .....
    System.exit(0);
  }
	
  public void addCars(BufferedReader inFile)
  {
    //This method works fine
    //read in car name (string) and car number (int) and add to ArrayList
		
    CarAttributes.add( new Car(name, number));
 
    //After I add to the list 4 times
    System.out.println(Car.size()); //works fine and displays 4
  }
 
  public void displayCars()
  {
    // this is were I'm have trouble
    // I need to pull in each car attribute (name:string, number(int) to display and/or manipulate
    // I've tried many things, but with either, error cannot convert primitive int, or 
    // a listing of memory referrences to each car in the list 
	
    }
}
 
public class Car
{
	private String carName;	
	private int carNumber
	
	public Car(String cName, int cNumber)
	{
		// Constructor
		this.setCarName(cName);
		this.setCarNumber(cNumber);
		// End Constructor
	}		
	
	public String getCName()
	{
		return this.carName;
	}
	
	public int getCNumber()
	{
		return this.cNumber;
	}
	
	public void setCarName(String cName)
	{
		this.carName = cName;
	}
	
	public void setCarNumber(int cNumber)
	{
		this.carNumber = cNumber;
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Enview
Enview

ASKER

Your solution worked great! Thank You!