List of items with same values

Posted by user559780 on Stack Overflow See other posts from Stack Overflow or by user559780
Published on 2011-01-01T13:38:52Z Indexed on 2011/01/01 13:54 UTC
Read the original article Hit count: 195

Filed under:
|

I'm creating a list of items from a file

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("H:/temp/data.csv")));
try {
    List<Item> items = new ArrayList<Item>();
    Item item = new Item();

    String line = null;
    while ((line = reader.readLine()) != null) {
        String[] split = line.split(",");

        item.name = split[0];
        item.quantity = Integer.valueOf(split[1]);
        item.price = Double.valueOf(split[2]);
        item.total = item.quantity * item.price;

        items.add(item);
    }

    for (Item item2 : items) {
        System.out.println("Item: " + item2.name);
    }
} catch (IOException e) {
    reader.close();

    e.printStackTrace();
}

Problem is the list is displaying the last line in the file as the value for all items.

© Stack Overflow or respective owner

Related posts about java

Related posts about list