[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

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

0
Enview
Asked:
Enview
  • 2
1 Solution
 
sciuriwareCommented:
You can 'get' all cars by:

    for(Car c : CarAttributes)
    {
         String name = c.getCName();
         int number = c.getCNumber();
    }


;JOOP!
0
 
EnviewAuthor Commented:
Your solution worked great! Thank You!
0
 
sciuriwareCommented:
:)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now