I am very new to coding and I cannot figure out where my error is, though I am sure (or, hoping, anyways) it will be obvious to you all.
My issue occurs at the very end. My code never reaches/runs the last while loop. The purpose of the program is to collect a list of ingredients for a recipe, and have the list of ingredients at the end. Does it have anything to do with the 'while' loops I used earlier? Thanks for the help. I feel like my brain is fried; I'm really not sure what else to try. (I apologize for my long post!)
If I reply with 'n', as as seen below, my code ends right there.
else if (reply.equals("n")) {
System.out.println("");
break;
Problem is, it's completely skipping the remainder of my code:
} while (addMoreIngredients);
for (int i = 0; i < ingredientList.size(); i++) {
String ingredient = ingredientList.get(i);
System.out.println("Your ingredients are " + ingredientList);
System.out.println("Goodbye");
}
Below is my entire code.
package SteppingStones;
import java.util.ArrayList;
import java.util.Scanner;
public class Ingredient {
public static void main(String[] args) {
String nameOfRecipe = "";
String nameOfIngredient = "";
float ingredientAmount = 0;
String unitMeasurement = "";
String Unit = "";
double numberUnits = 0.0;
int numberCaloriesPerUnit = 0;
double totalCalories = 0.0;
int count = 0;
String newIngredient = "";
boolean addMoreIngredients = true;
ArrayList<String> ingredientList = new ArrayList();
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter name of recipe");
nameOfRecipe = scnr.nextLine();
System.out.println("Please ener the name of first ingredient");
nameOfIngredient = scnr.next();
while (!nameOfIngredient.matches("[a-zA-Z_]+")) {
System.out.println("Error. Letters only. Please, try again.");
nameOfIngredient = scnr.nextLine();
}
System.out.println("Please enter the unit of measurement");
unitMeasurement = scnr.next();
while (!unitMeasurement.matches("[a-zA-Z_]+")) {
System.out.println("Error. Invalid response. Letters only");
unitMeasurement = scnr.nextLine();
}
System.out.println("Please enter the number of " + unitMeasurement +
" of " + nameOfIngredient + " we'll need.");
Unit = scnr.nextLine();
while(true) {
try {
do{
numberUnits = Float.parseFloat(scnr.nextLine());
if (numberUnits > 0.0 && numberUnits <= 100.0) {
System.out.println("Valid response");
count++;
}
else {
System.out.println("Error. Invalid response. Please enter a number between 1
and 100");
System.out.println("Please enter the number of "
+ unitMeasurement + " of " + nameOfIngredient + "we'll need.");
}
}while(count<0);
break;
}
catch(NumberFormatException e) {
System.out.println("Error. Invalid input");
}
}
System.out.println("Please enter the calories per unit.");
while(true) {
try {
numberCaloriesPerUnit = Integer.parseInt(scnr.next());
break;
}
catch(NumberFormatException e) {
System.out.println("Error. Invalid response");
}
}
totalCalories = numberCaloriesPerUnit * numberUnits;
System.out.println(nameOfIngredient + " uses " + numberUnits + " " + unitMeasurement + " and has "
+ totalCalories + " calories.");
OUTER:
do {
System.out.println("Would you like to enter an ingredient: (y or n)" );
String reply = scnr.next().toUpperCase();
if (reply.equals("y")) {
System.out.println("Enter the name of the ingredient: ");
newIngredient = scnr.next();
while (!newIngredient.matches(".*[a-z].*"))
ingredientList.add(newIngredient);
System.out.println("Enter unit of measurement: ");
unitMeasurement = scnr.next();
System.out.println("Enter amount of ingredient: ");
ingredientAmount = scnr.nextInt();
continue;
}
else if (reply.equals("n")) {
System.out.println("");
break;
}
else {
System.out.println("Error. Please enter y or n!");
continue;
}
} while (addMoreIngredients);
for (int i = 0; i < ingredientList.size(); i++) {
String ingredient = ingredientList.get(i);
System.out.println("Your ingredients are " + ingredientList);
System.out.println("Goodbye");
}
}
}
Aucun commentaire:
Enregistrer un commentaire