Home > database >  Add to an ArrayList using the get method
Add to an ArrayList using the get method

Time:06-19

What is the most appropriate way to add to an ArrayList using values from the get method.

I'm aware the below line of code is an error, I can't figure what's wrong in it.

System.out.println("List of Foods");
List<String> foodList = new ArrayList<>();
foodList.addAll(getId(), getDescription(), getIngredients(), getSellingPrice());
System.out.println(foodList);

Error Message:

 Multiple markers at this line
    - Occurrence of 'addAll'
    - The method addAll(int, Collection<? extends String>) in the type List<String> is not applicable 
     for the arguments (int, String, List<String>, double)
    - 1 changed line

The idea behind is to be able to retrieve these user input in the main method and display it using the below code

System.out.println("---------------------------------------------------------------------------");
System.out.printf("%5s s  s 0s", "FOOD ID", "DESCRIPTION", "SELLING PRICE", "INGREDIENTS"   "\n"); 
System.out.println("---------------------------------------------------------------------------");

//iterates over the list   
for (Food food : foods) {
    System.out.format("%5s s  s 0s", food.getId(), food.getDescription(), food.getSellingPrice(), food.getIngredients());
    System.out.println(); 
}
System.out.println("---------------------------------------------------------------------------");

Any help will be much appreciated.

get() methods definition:

public int getId() {
    return id;
}

public String getDescription() {
    return description;
}

public double getSellingPrice() {
    return sellingPrice;
}

public List<String> getIngredients() {
    return ingredients;
}

I have changed the following line of codes:

System.out.println("List of Foods");
                List<String> foodList = new ArrayList<>();
                foodList.add(getId());
                foodList.add(getDescription());
                foodList.addAll(getIngredients());
                foodList.add(getSellingPrice());
                System.out.println(foodList);

and I have changed the existing Food class:

public class Food {

private String  id;
private String description;
private String sellingPrice;
private List<String> ingredients;

Scanner sc = new Scanner(System.in);

public Food(String id, String description, String sellingPrice, List<String> ingredients) {
    super();
    this.id = id;
    this.description = description;
    this.sellingPrice = sellingPrice;
    this.ingredients = ingredients;

CodePudding user response:

Assume the getId(), getDescription(), getIngredients(), getSellingPrice() methods was reading user input.

You will need to create a Food class to hold these values and have an ArrayList of Food for the display logic.

Pseudo code

public class Food{
    private String id;
    private String description;
    private String ingredients;
    private double sellingPrice;
    // Constructor, Getter, Setter
}


List<Food> foodList = new ArrayList<>();
String id = getId()
String description = getDescription()
String ingredients = getIngredients()
double sellingPrice = getSellingPrice()
Food food = new Food(id, description, ingredients, sellingPrice)

foodList.add(food);   

CodePudding user response:

Make sure you add lines. List<String> - must contain strings. Convert to strings. What type do we return the methods that you put in to the List<>.

  • Related