简体   繁体   中英

Using Loop to Generate ArrayList

In the code below, I am attempting to capture name, cost, and priority into an object. I use a for loop to create 7 objects and add each object to an ArrayList called itemData.

I do not think my code is retaining name, cost and priority into an item each time the loop is run. Any suggestions are greatly appreciated.

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);
       itemData.add(grocItem); // add grocery items to itemData ArrayList

   };

  System.out.println(item.name)

  // System.out.println(+ grocItem);
    }
    //How do I add grocItem to an Array list of other grocItems (6 grocItems from user input array item)
    //Main.itemData.add(groclist);


}


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

// Main.groclist.add(grocItem);

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-

for(ItemData someItem : itemData){
    System.out.println("\nName: " + someItem.name + "\tCost: " + someItem.cost + "\tPriority: " + someItem.priority);
}

You can improve your code by a long shot.

EDIT:

If you are willing to learn, here's a better version of what you are trying to do-

Let's define ItemData (Save ItemData below in ItemData.java file)-

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;
    }
}

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)-

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<ItemData> itemData = new ArrayList<ItemData>();

    private void setUpList(){

        Scanner keyboard = new Scanner(System.in);

        for (int i = 0; i < 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();

    }

}

To compile, do-

javac GroceryProgram.java

To execute-

java GroceryProgram

No questions?

Delete these lines - you're not using item so remove it entirely from your code:

String[] item  = new String[7];

System.out.println(item.name)

Now that you don't have item , fix the for loop:

for (i=0; i<7; i++) { // just code the loop to iterate 7 times

Next add a toString() method to ItemData , which is necessary for the following step to print readable output.

// return what you want - this is a reasonable example
public String toString() {
    return name + ", " + cost + " - priority " + priority;
}

Finally, print the list after the loop:

System.out.println(itemData};

That should pretty much fix up the problems you're having right now.

String name = keyboard.next();

Is name ever more than two words? You might want to use keyboard.nextLine()

This line is not correct " System.out.println(item.name)" . Item is an array.

Secondly you are not storing anything in array but in arraylist which is ' itemData' .

So try to fetch value from itemdata . For eg: itemData.get(0).name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM