Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should try making a User Defined Type which acts as a data structure to hold the values you desire.</p> <p>Since java is an object oriented language, you can create a user defined type with a class. Below, I created a class called Item with three member data variables. This class is a blueprint for all Item type objects. Whenever you create a new item object, it will have its own unique copies of the member data variables.</p> <p>For sake of simplicity, I did not encapsulate the member data variables, but for future programs you should declare member data variables private and provide an interface to access/modify them. Leaving them public here allows me the convenience of accessing them with the dot (.) operator.</p> <p>See the following code for a working example</p> <pre><code>import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Item { public String name; public int priority; public double cost; public static void main(String[] args) { int itemAmount = 7; List&lt;Item&gt; itemList = new ArrayList&lt;Item&gt;(); for (int i=0; i &lt; itemAmount; i++) { Item itemToCreate = new Item(); Scanner keyboard = new Scanner(System.in); System.out.println("Enter item name " + i); itemToCreate.name = keyboard.next(); Scanner keyboard2 = new Scanner(System.in); System.out.println("Enter the price of item " + i); itemToCreate.cost = keyboard2.nextDouble(); Scanner keyboard3 = new Scanner(System.in); System.out.println("Enter Priority Number " + i); itemToCreate.priority = keyboard3.nextInt(); itemList.add(itemToCreate); } // end for for (int i=0; i &lt; itemList.size(); i++) { Item tempItem = itemList.get(i); System.out.println("Item " + tempItem.name + " has cost " + tempItem.cost + " with priority " + tempItem.priority); } } // end main } // end class </code></pre> <p>You should notice I replaced your original declaration with an ArrayList data structure. This is a dynamic data structure that can grow as you insert items into it. You should take on the challenge of allowing for unlimited amount of user input and to account for an unknown quantity of Item objects in your list. </p> <p>Hope this helps!</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