vendredi 11 juin 2021

Can While loops cause code to skip Boolean loop?

I apologize ahead of time for the length of this question. But I don't think it can be made any shorter. I am very new to coding, so forgive me if my question seems novice. I believe my collection of While loops are preventing my Boolean loop from running. Therefore, my code ends early. However, my While loops serve the purpose of variable validation. Any other ideas how I can validate input without While loops/without it disrupting my Boolean loop?

I think one/all of these loops are my problem:

 while (!nameOfIngredient.matches("[a-zA-Z_]+")) {
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 (!newIngredient.matches("[a-zA-Z_]+"))

With the While loops, my code ends early at this line:

else if (reply.equals("n")) {
                  System.out.println("");
                break;
       }

My boolean loop is at the very end and is, therefore, being skipped:

} 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");
       } 

This is my code in it's entirety (again, sorry for length).

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().toLowerCase(); 
        if (reply.equals("y")) {
               System.out.println("Enter the name of the ingredient: "); 
               newIngredient = scnr.next();  

               while (!newIngredient.matches("[a-zA-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 " + nameOfIngredient + ingredientList);
           System.out.println("Goodbye");
       }      
    }
 }

Thank you!!!

Aucun commentaire:

Enregistrer un commentaire