0

I am going to create a list that I want to add elements to. I have obtained the elements from clients via them completing 3 dialogue boxes indicating their preferences for a new car, and now I want to add these entries to the list.

I have declared my instance variables and here is the constructor for my list:

 public Car(String aMaker, String aModel, int aYearBuilt);
    {
        super();
        this.maker = aMaker;
        this.model = aModel;
        this.year = aYearBuilt;
    }

I have successfully put the return from my first client's dialogue boxes into 3 variables, created thus:

 String inputMaker;
 String inputModel;
 int inputYear; 

(I remembered to use Integer.parseInt to convert the dialogue input string to an int).

Now I want to put the values in a list:

List<Car> newCarDetails = new ArrayList<Car>();

This is where I start to go wrong:

 newCarDetails.add(new Car(inputMaker, inputModel, inputYear));
    return newCarDetails;

Only I have clearly underestimated the task, because it doesn't work. I just get a hashCode back. Any help greatly appreciated.

Ellie
  • 27
  • 4
  • can you edit your code to be more readable please? – aseychell Apr 12 '11 at 17:09
  • Sorry, new & not quite got the hang of it all yet. – Ellie Apr 12 '11 at 17:12
  • `String input.Model;` isn't a valid variable declaration... what does your code actually look like? – Jon Skeet Apr 12 '11 at 17:14
  • It is not clear from your question where you are getting "hashCode". Could you add some more code that illustrates where you are seeing it? – jt. Apr 12 '11 at 17:14
  • 1
    I don't get it. What are you trying to do? – genobis Apr 12 '11 at 17:15
  • Sorry Jon, that was a typo that had crept in - fixed now. – Ellie Apr 12 '11 at 17:17
  • @genobis - The items for the list have to be input from the client via a dialog box. What I'm attempting is to then enter them into the list. I thought assigning them each to a variable then adding them might work. – Ellie Apr 12 '11 at 17:19
  • @Ellie: You haven't shown enough of your code to know what's going on - please read http://tinyurl.com/so-hints. It's possible that my answer will already help, but it's worth knowing how to write a useful question for the future :) – Jon Skeet Apr 12 '11 at 17:24
  • Sorry, I was trying to make it succinct. – Ellie Apr 12 '11 at 17:31

4 Answers4

3

Your code looks fine to me. You're adding the new Car to a List<Car> that you've previously created. If by "hash code" you mean that when you do System.out.println(newCarDetails) you get some output like <java.util.List<0x123456>> that's just what happens when you try to print any class that doesn't have a toString() method. A lot of the Collections framework classes don't.

If you want to pretty-print it, try the technique here.

Community
  • 1
  • 1
Jonathan
  • 7,536
  • 4
  • 30
  • 44
  • 1
    Right. That's very helpful. As a complete novice I had assumed that what I'd written would return the 'String, string, int' input that had originally been in the 3 variables. So it's likely that I'm not providing an appropriate toString() method. Thank you, that gives me somewhere to go. – Ellie Apr 12 '11 at 17:28
0

Use a typed list:

List<Car> newCarDetails = new ArrayList<Car>();

newCarDetails.add(new Car(inputMaker, inputModel, inputYear));
Bernard
  • 7,908
  • 2
  • 36
  • 33
0

I don't know what your output media is, but you could iterate over the list to format the output:

for (Car newCar : newCarDetails) {
  System.out.println("Make: " + newCar.getAMaker());
  System.out.println("Model: " + newCar.getAModel());
  System.out.println("Year: " + newCar.getAYearBuilt());
}

Alternatively, you could override Car's toString() method to regulate the formatting

class Car {
...

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(this.aMaker);
    sb.append(",");
    sb.append(this.aModel);
    sb.append(",");
    sb.append(this.aYearBuilt);
    sb.append(",");

    return sb.toString();
  }
}

...

for (Car newCar : newCarDetails) {
  System.out.println(newCar.toString());
}
Mike Yockey
  • 4,565
  • 22
  • 41
0

You haven't actually said what you're trying to do with the returned value, or how it's really behaving, only this:

Only I have clearly underestimated the task, because it doesn't work. I just get a hashCode back. Any help greatly appreciated.

Hmm. My psychic debugging skills suggest that you're calling toString on either the ArrayList<Car> or the Car itself, and that's returning something which doesn't look terribly useful.

Options:

  • Use the individual values within the Car when you're formatting (e.g. by calling the properties)
  • Override toString in the Car class with your chosen format

If neither of those are useful, please edit your question to give more information about what you're doing with the list...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194