Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Array from multiple user inputs of different types

How I would get the 3 items from user input Item (name, cost, priority) into one object item and place that object item into an array of other objects?

public class Main {

    public static void main(String[] args) {
       //here are my variables.  
       //I'm pretty sure a few of these should be defined 
       //  in their own item class. 
       int i=0;
       String name1;
       int priority1; 
       double cost1;

       //String[] shoppingList = new String[7];
       String[] item  = new String[7];

       // I am able to grab input from the user for all 3 elements of 
       //   an item object (name, cost, priority)
       // I am missing how to save these 3 elements into an item object 
       //  and create an array for each item object
       for (i=0; i<item.length;i++)  {

          //I want to capture name, priority, and cost of each item
          //  and add each item as an in
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Enter item name " + i);
           String name = keyboard.next();

           Scanner keyboard2 = new Scanner(System.in);
           System.out.println("Enter the price of item " + i);
           double cost = keyboard2.nextDouble();

           Scanner keyboard3 = new Scanner(System.in);
           System.out.println("Enter Priority Number " + i);
           int priority = keyboard3.nextInt();
like image 798
user2558595 Avatar asked Dec 07 '22 06:12

user2558595


2 Answers

"How I would get the 3 items from user input Item (name, cost, priority) into one object item"

If I understand you correctly, the solution would be to make a new class. Call it whatever is relevant for what you're doing

class Data {
    String name;
    double cost;
    int priority;
    Data(String name, double cost, int priority) { //Set variables }
}

Then you can create a new "Data" class with your relevant information with:

Data myObject = new Data("Input1", 37.50, 2);

Just for example, with random information. You can make as many of these unique objects as you'd like, and add them to an array or ArrayList depending on what you want to do:

Data[] myArray = new Data[100];
//or
List<Data> myList = new ArrayList<Data>();
like image 147
Kon Avatar answered May 17 '23 13:05

Kon


You could try a custom class:

public class Item {
    private String name;
    private double price; // you should probably use an 'int' for this
                          // it would be the amount of cents
                          // that would avoid rounding errors
    private int priority;
    public Item(String name, double price, int priority) {
        this.name = name;
        this.price = price;
        this.priority = priority;
    }
    // (getters and setters)
    public String getName() {
        return name;
    }
    ...
}

Item milk = new Item("milk", 19.99, 3); // wow, expensive milk :P
Item[] items = [milk, new Item("cheese", 99.99, 2), ...];
like image 23
tckmn Avatar answered May 17 '23 13:05

tckmn