1

I cannot get the Product Objects to print out anything using an Enhanced for loop. Everything comes out null or 0?

The output show this?

0null0.0This is the id
0null0.0This is the id
0null0.0This is the id

Here's my code:

class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
        ArrayList < Product > store1 = new ArrayList < Product > ();
        store1.add(new Product(3, "Nike", 300.0));
        store1.add(new Product(2, "Addidas", 400.0));
        store1.add(new Product(6, "Under Armor", 500.0));
        for (Product y: store1) {
            System.out.println(y + "This is the id");
        }
    }
}

class Product {
    public int id;
    public String name;
    public double price;
    public Product(int startId, String startName, double startPrice) {
        startId = id;
        startName = name;
        startPrice = price;
    }
    public int getId() {
        return id;
    }
    public double getPrice() {
        return price;
    }
    public String getName() {
        return name;
    }
    public String toString() {
        return id + name + price;
    }
}
9
  • Here is the main: Commented Apr 16, 2019 at 14:24
  • 1
    Hi! When you were asking your question, there was a How to Format box to the right of the text area with useful info in it. There was also a toolbar full of formatting aids. And a [?] button giving formatting help. And a preview area showing what your post would look like when posted, located between the text area and the Post Your Question button (so that you'd have to scroll past it to find the button, to encourage you to look at it). Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. Commented Apr 16, 2019 at 14:24
  • 1
    Please use the "edit" link to improve the question. Don't post code in comments. Commented Apr 16, 2019 at 14:25
  • 1
    Please edit your question, don't paste the code in the comments as you see, it gets messy Commented Apr 16, 2019 at 14:25
  • I've put the code in the question for you this time. Commented Apr 16, 2019 at 14:27

3 Answers 3

2

You are doing a backward assignments in the constructor:

public Product(int startId, String startName, double startPrice) {
        startId = id;
        startName = name;
        price = startPrice;
    }

leaving the object uninitialized...

but you mean for sure

public Product(int startId, String startName, double startPrice) {
        id = startId;
        name = startName;
        startPrice = price;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yup. That was it!!! Thanks so much. I messed up my instance variable assignments!
1

You have your assignments backward in the constructor. It should be:

public Product(int startId, String startName, double startPrice) {
    id = startId;       // Not `startId = id;`
    name = startName;   // Not `startName = name;`
    price = startPrice; // Not `price = startPrice;`
}

or better yet (and this would have flagged the problem up for you when you tried to compile), don't rely on implicit this:

public Product(int startId, String startName, double startPrice) {
    this.id = startId;
    this.name = startName;
    this.price = startPrice;
}

Comments

0

You are setting the variables the wrong way around in your constructor, i.e.

startId = id; should be id = startId;

You should also add @Override to your toString() method.

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.