0

I am currently having trouble getting my while loop to successfully let the user input a 2nd string into an arraylist. On the 2nd pass of the loop it skips user input for the addNewIngredient() method and goes straight to the addNewCost() method.

Here is my class

import java.util.ArrayList;
import java.util.Scanner;

public class Recipe {

ArrayList<String> ingredientList = new ArrayList<>(); 
ArrayList<Double> costPerServing = new ArrayList<>();

double price;
boolean quit = true;

Scanner keyboard = new Scanner(System.in);

This is the method that initializes the loop sequence.

 public void createRecipe(){
        
        while(quit)
        {
            addNewIngredient();
            addNewCost();            
            displayCurrent();    
            promptNew();
        }
        
        System.out.println("Enter your price for the menu item.");
        price = keyboard.nextDouble();
        
    }
    

Initially, I used except this didn't allow the user to include a space in their ingredient name.

ingredientList.add(keyboard.next());

This is currently the method that won't allow the user to input another string into the next index of the arraylist.

public void addNewIngredient(){
   
    System.out.println("Enter an Ingredient");
    ingredientList.add(keyboard.nextLine());
    System.out.println();
               
}

I also tried adding the users string to a local string variable first.

public void addNewIngredient(){
    String ing;

    System.out.println("Enter an Ingredient");
    ing = keyboard.nextLine();

    ingredientList.add(ing);
    System.out.println();
               
}

Any help would be appreciated.

0 Answers0