0

I have been working on this for a while, but can't quite seem to figure out how to add an item to an ArrayList. I would like to add grocItem (should be 7 grocItems from user input from for loop) into an ArrayList of grocList:

        public class ItemData{
        public ItemData(String name, double cost, int priority){

        Main.(ArrayList grocList).add(grocItem);

          // Main.groclist.add(grocItem);



    }
}

Main Class:

import java.util.*;


public class Main {

    public static List<ItemData> itemData = new ArrayList<ItemData>();
    public static void main(String[] args) {
        int i=0;
        //String name1;
        //int priority1; 
        //double cost1;

       String[] item  = new String[7];

        for (i=0; i<item.length; i++)  {
            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();

            ItemData grocItem = new ItemData(name, cost, priority);
        }
        //How do I add grocItem to an Array list of other grocItems (6 grocItems from user input array item)
        Main.itemData.add(groclist);

    }
}

2 Answers 2

5

Change your code, add the method inside the loop.

for (i=0; i<item.length; i++)  {
            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();

            ItemData grocItem = new ItemData(name, cost, priority);
            itemData.add(grocItem ); // add here 
        }
Sign up to request clarification or add additional context in comments.

Comments

1

You should add the ItemData object to your arraylist inside your loop:

for (i=0; i<item.length; i++)  {
        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();

        ItemData grocItem = new ItemData(name, cost, priority);
        itemData.add(groclist); // <-- add to arraylist inside the loop

}

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.