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.

9
  • can you edit your code to be more readable please? Commented Apr 12, 2011 at 17:09
  • Sorry, new & not quite got the hang of it all yet. Commented Apr 12, 2011 at 17:12
  • String input.Model; isn't a valid variable declaration... what does your code actually look like? Commented Apr 12, 2011 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? Commented Apr 12, 2011 at 17:14
  • 1
    I don't get it. What are you trying to do? Commented Apr 12, 2011 at 17:15

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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());
}

3 Comments

Thanks, I'll have a look at that possibility.
I added a method that is arguably better by overriding Car's toString() method.
Haha! Those last 2 lines did it for me! Thanks v much :) :)
0

Use a typed list:

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

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

2 Comments

Yes it was, it just wasn't in a code block, and HTML eats anything between < and > that isn't in a code block.
@R. Bemrose: You are correct, but because of this I wasn't able to see it initially.
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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.