Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your list is fine. It's just that you are trying to print the items of the list wrong way. You should do something like, say after your for loop-</p> <pre><code>for(ItemData someItem : itemData){ System.out.println("\nName: " + someItem.name + "\tCost: " + someItem.cost + "\tPriority: " + someItem.priority); } </code></pre> <p>You can improve your code by a long shot.</p> <p><strong>EDIT:</strong></p> <p>If you are willing to learn, here's a better version of what you are trying to do-</p> <p>Let's define ItemData (Save ItemData below in ItemData.java file)-</p> <pre><code>public class ItemData { private String name; private double cost; private int priority; public ItemData(String name, double cost, int priority){ this.name = name; this.cost = cost; this.priority = priority; } public void setName(String name){ this.name = name; } public void setCost(double cost){ this.cost = cost; } public void setPriority(int priority){ this.priority = priority; } public String getName(){ return this.name; } public double getCost(){ return this.cost; } public int getPriority(){ return this.priority; } } </code></pre> <p>Now that we have our ItemData, we can use it like (Save GroceryProgram below in a file named GroceryProgram.java in the same directory as ItemData.java)-</p> <pre><code>import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class GroceryProgram { private final static int GROC_SIZE = 6; private final List&lt;ItemData&gt; itemData = new ArrayList&lt;ItemData&gt;(); private void setUpList(){ Scanner keyboard = new Scanner(System.in); for (int i = 0; i &lt; GROC_SIZE; i++) { System.out.print("\nEnter item name (" + i + ") : "); String name = keyboard.next(); System.out.print("\nEnter the price of item (" + i + ") : "); double cost = keyboard.nextDouble(); System.out.print("\nEnter Priority Number (" + i + ") : "); int priority = keyboard.nextInt(); ItemData grocItem = new ItemData(name, cost, priority); itemData.add(grocItem); // add grocery items to itemData ArrayList } keyboard.close(); } private void displayListItems(){ for(ItemData someItem : itemData){ System.out.println("\nName: " + someItem.getName() + "\tCost: " + someItem.getCost() + "\tPriority: " + someItem.getPriority()); } } public static void main(String[] args) { GroceryProgram groProgram = new GroceryProgram(); groProgram.setUpList(); groProgram.displayListItems(); } } </code></pre> <p>To compile, do-</p> <blockquote> <p>javac GroceryProgram.java</p> </blockquote> <p>To execute-</p> <blockquote> <p>java GroceryProgram</p> </blockquote> <p>No questions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload