Question
So I came up with the code below based and when I run the code it gets stuck on asking would you like to enter
So I came up with the code below based and when I run the code it gets stuck on asking would you like to enter an ingredient yes or no. I have to go following a guideline provided by my professor, but I'm stuck as to what I'm doing wrong and why it won't recognize y or n or anything when I enter it in.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package SteppingStones;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author kat10
*/
public class SteppingStone4_Loops {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String recipeName = "";
ArrayList
String newIngredient = "";
boolean addMoreIngredients = true;
System.out.println("Please enter the recipe name: ");
recipeName = scnr.nextLine();
do {
System.out.println("Would you like to enter an ingredient: (y or n)");
String reply = scnr.next().toLowerCase(); //checks to see if user entered y or n
/**Add your code here (branches work well!). The code should check the
* reply:
*"y" --> prompt for the ingredient and add it to the ingredient list;
*"n" --> break out of loop;
*(Hint: what is the 'while' condition? What could you change to
*stop the loop from starting over?)
*anything else --> prompt for a "y" or "n"
*
*/
if (reply.equals('y')) { //if y is entered it prompts user to enter ingredient name
System.out.println("Enter ingredient name: ");
newIngredient = scnr.nextLine();
ingredientList.add(newIngredient); //stores into ingredient
}
if (reply.equals('n')) {
addMoreIngredients = false;
}
else {
System.out.println("Would you like to enter an ingredient: (y or n)"); //prompts user to enter y or n again
}
}
while (addMoreIngredients);
for (int i = 0; i < ingredientList.size(); i++) {
/**
* Get the i from the ingredient list
* and assigning it to the String ingredient
*
*/
String ingredient = (String)ingredientList.get(i); //gets ingredient from list and stores it in ingredient
System.out.println(ingredient); //prints ingredient
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started