Reputation: 55
I'm confused about something, again, in my code. I'm a beginner, but I still am familiar with the Scanner class. However, for a generic array, it bypasses the user input of a string. I want to understand why it's not being "collected" for the ArrayList:
public static void addingIngredients(){
ArrayList<String> Ingredients = new ArrayList<String>();
String addedIngredient = input.nextLine();
Ingredients.add(addedIngredient);
System.out.println(Ingredients +": Continue?" );
System.out.println("1 (Yes) / 2 (No)");
int choice = input.nextInt();
switch (choice){
case 1:
addingIngredients();
case 2:
System.out.println("Test");
}
}
The String "addedIngredient" is being skipped; This is what I'm receiving via console:
Now Loading...
What Ingredients are in this protein powder?
Begin Add?
1 (Yes) / 2 (No)
1
[]: Continue?
1 (Yes) / 2 (No)
1
[]: Continue?
1 (Yes) / 2 (No)
2
Test
Thank you in advance. P.S: Is there a more convenient way to write a loop to collect data from the user?
Upvotes: 0
Views: 77
Reputation: 24192
youre creating a new ArrayList with every call. you need to either use the same one of collect the results of each recursive call.
here's a simpler version you can start with:
ArrayList<> ingredients = ...
while (true) {
//primpt for ingrediant, add to list
if (endOfInput()) { //this is where you prompt for 1/2
break;
}
}
Upvotes: 2