Question
Trying to create an if/else branch that will notify the user of an invalid entry when creating a recipe, allowing them another chance, instead of
Trying to create an if/else branch that will notify the user of an invalid entry when creating a recipe, allowing them another chance, instead of just stopping the code when entering not a string or integer. Any help appreciated! Thanks in advance!!
Right now, the program will stop and generate an error if the user enters anything other than a string input into the recipe name, or anything other than an integer input into the other recipe parameters. What I would like it to do is open another if/else branch that will verify if the correct input is entered, and say "invalid entry" if not, allowing another attempt. And possibly step through a loop that will continue this until a valid entry type is made. I just can't seem to get the inner loops to accomplish this to work and could use some help. I removed the loops I developed for this in my code below, to just provide what I have that does work. I appreciate any help!
package steppingstones;
//import classes
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author
*/
public class SteppingStone6_RecipeBox {
//instance variables
private ArrayList
// accessors for the list Of Recipes
public ArrayList
{
return listOfRecipes;
}
// mutators for the list Of Recipes.
public void setListOfRecipes(ArrayList
{
this.listOfRecipes = listOfRecipes;
}
// default constructors for the SteppingStone6_RecipeBox()
public SteppingStone6_RecipeBox()
{
this.listOfRecipes = new ArrayList<>();
}
// parameter constructors for the SteppingStone6_RecipeBox()
public SteppingStone6_RecipeBox(ArrayList
{
this.listOfRecipes = listOfRecipes;
}
// custom method named printAllRecipeDetails
void printAllRecipeDetails(String selectedRecipe)
{
for (SteppingStone5_Recipe recipe : listOfRecipes)
{
if (recipe.getRecipeName().equalsIgnoreCase(selectedRecipe))
{
recipe.printRecipe();
return;
}
}
System.out.println("No Recipe found with name: " + selectedRecipe);
}
// custom method named PrintAllRecipeNames()
void printAllRecipeNames()
{
for (SteppingStone5_Recipe selectedRecipe : listOfRecipes)
{
System.out.println(selectedRecipe.getRecipeName());
}
}
// method named AddRecipe().
public void addRecipe(SteppingStone5_Recipe tempRecipe)
{
listOfRecipes.add(tempRecipe);
}
// main method to execute the program
public static void main(String[] args)
{
// Create a Recipe Box
SteppingStone6_RecipeBox myRecipeBox = new SteppingStone6_RecipeBox();
Scanner menuScnr = new Scanner(System.in);
//Printing menu for a user to select the available options
System.out.println("Menu " + "1. Add Recipe " +
"2. Print All Recipe Details " + "3. Print All Recipe Names " +
" Please select a menu item:");
while (menuScnr.hasNextInt() || menuScnr.hasNextLine())
{
int input = menuScnr.nextInt();
//Using if-else to select three custom methods.
if (input == 1) {
myRecipeBox.newRecipe();
}
else if (input == 2) {
System.out.println("Which recipe? ");
String selectedRecipe = menuScnr.next();
myRecipeBox.printAllRecipeDetails(selectedRecipe);
}
else if (input == 3) {
for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++)
{
System.out.println((j + 1) + ": " +
myRecipeBox.listOfRecipes.get(j).getRecipeName());
}
}
else {
System.out.println(" Menu " + "1. Add Recipe " +
"2. Print Recipe " + "3. Adjust Recipe Servings " +
" Please select a menu item:");
}
System.out.println("Menu " + "1. Add Recipe " +
"2. Print All Recipe Details " +
"3. Print All Recipe Names " +
" Please select a menu item:");
}
}
// Create a method named 'newRecipe()'
public void newRecipe()
{
double totalRecipeCalories = 0.0;
ArrayList
boolean addMoreIngredients = true;
Scanner scnr = new Scanner(System.in);
//prompt user to enter recipe name.
System.out.println("Please enter the recipe name: ");
String recipeName = scnr.next();
//prompt user to enter number of servings for recipe.
System.out.println("Please enter the number of servings: ");
int servings = scnr.nextInt();
do
{
//prompt user to enter ingredients or type end to finish loop.
System.out.println("Please enter the ingredient name or type end if you are finished entering ingredients: ");
String ingredientName = scnr.next();
ingredientName = ingredientName.toLowerCase();
//if user type end loop will end and print the recipe with ingredient, serving and calories.
if (ingredientName.toLowerCase().equals("end"))
{
addMoreIngredients = false;
}
else
{
//if user enter the ingredient loop will start to ask for ingredient details.
recipeIngredients.add(ingredientName);
System.out.println("Please enter an ingredient amount: ");
Double ingredientAmount = scnr.nextDouble();
//prompt user to enter measuring unit of ingredient
System.out.println("Please enter the measurement unit for an ingredient: ");
String unitMeasurement = scnr.next();
//prompt user to enter ingredient calories
System.out.println("Please enter ingredient calories: ");
int ingredientCalories = Integer.parseInt(scnr.next());
//Expression to calculate total calories
totalRecipeCalories = (ingredientCalories * ingredientAmount);
//prints the ingredient name with required amount, measuring
// unit and total calories.
System.out.println(ingredientName + " uses " + ingredientAmount + " " +
unitMeasurement + " and has " + totalRecipeCalories + " calories.");
} //end else loop.
} while (addMoreIngredients);
// Now create a recipe object and add it to the list
SteppingStone5_Recipe recipe = new SteppingStone5_Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories);
addRecipe(recipe);
}
}
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