Question
What is wrong with my code, there is an error with my loop for amount. The code gets an error there. Code: import java.util.Scanner; public
What is wrong with my code, there is an error with my loop for amount. The code gets an error there.
Code:
import java.util.Scanner; public class Ingredients { //instance variables and properties private String nameOfIngredient; //string for user to end words for ingredients private String unitMeasurement; //string for user to enter words for unit of measure private float ingredientAmount; //float used for rounded decimal of amount private int numberCaloriesPerUnit; //integer for user to enter whole number for calories private float totalCalories; //float used for rounded decimal of calories //constructor with no inputs public Ingredients(){ this.nameOfIngredient = ""; this.unitMeasurement = ""; this.ingredientAmount = 0.0f; this.numberCaloriesPerUnit = 0; this.totalCalories = 0.0f; } //constructor with inputs public Ingredients(String nameOfIngredient, String unitMeasurement, float ingredientAmount, int numberCaloriesPerUnit, float totalCalories){ this.nameOfIngredient = nameOfIngredient; this.unitMeasurement = unitMeasurement; this.ingredientAmount = ingredientAmount; this.numberCaloriesPerUnit = numberCaloriesPerUnit; this.totalCalories = totalCalories; } //return the name of the ingredient public String getNameOfIngredient(){ return this.nameOfIngredient; } //return the unit of measurement public String getUnitMeasurement(){ return this.unitMeasurement; } //return the ingredient amount public float getIngredientAmount(){ return this.ingredientAmount; } //return the number of calories per unit public int getnumberCaloriesPerUnit(){ return this.numberCaloriesPerUnit; } //return total calories public float getTotalCalories(){ return this.totalCalories; } //set the name of ingredient public void setNameOfIngredient(String nameOfIngredient){ this.nameOfIngredient = nameOfIngredient; } //set the unit of measurement public void setUnitMeasurement(String unitMeasurement){ this.unitMeasurement = unitMeasurement; } //set the ingredient amount public void setIngredientAmount(float ingredientAmount){ this.ingredientAmount = ingredientAmount; } //set the number of calories per unit public void setNumberCaloriesPerUnit(int numberCaloriesPerUnit){ this.numberCaloriesPerUnit = numberCaloriesPerUnit; } //set total calories public void setTotalCalories(float totalCalories){ this.totalCalories = totalCalories; } //main method
/** * * @param args */ public static void main(String args[]){ //create new ingredient object Ingredients new_ingredient = new Ingredients();
//display all attributes /*System.out.println(new_ingredient.getNameOfIngredient() + " uses " + new_ingredient.getIngredientAmount() + " " + new_ingredient.getUnitMeasurement() + " and has " + new_ingredient.getTotalCalories() + " calories.");*/
String tempNameOfIngredient = ""; //initalize name of ingredient String tempUnitMeasurement = ""; //initialize unit of measurement float tempIngredientAmount = 0.0f; //initialize ingredient amount int tempNumberCaloriesPerUnit = 0; //intialize calories per unit float tempTotalCalories = 0.0f; //initalize total calories float MAX_AMOUNT = 15.0f; //initalize max amount of ingredient int MAX_CALORIES = 5000; //initialize max amount of calories Scanner scnr = new Scanner(System.in);
//Prompt the user for the name of ingredient System.out.println("Please enter the name of ingredient: "); tempNameOfIngredient = scnr.nextLine();
//while loop to validate input for name of ingredient while(!tempNameOfIngredient.matches("[a-zA-z]+")){ System.out.println("Please enter valid ingredient name."); tempNameOfIngredient = scnr.nextLine(); } new_ingredient.setNameOfIngredient(tempNameOfIngredient);
//Prompt the user for the ingredient amount System.out.println("Please enter the amount of " + tempNameOfIngredient + ": "); tempIngredientAmount = scnr.nextFloat();
//while loop to validate ingredient amount input while(tempIngredientAmount < 0 || tempIngredientAmount > MAX_AMOUNT){ System.out.println("Please enter valid ingredient amount."); tempIngredientAmount = scnr.nextFloat(); } new_ingredient.setIngredientAmount(tempIngredientAmount);
//Prompt the user for the unit of measurement System.out.println("Please enter the unit of measurement for " + tempNameOfIngredient + ": "); tempUnitMeasurement = scnr.next();
//while loop to validate the unit of measurement while(!tempUnitMeasurement.matches ("cups")|| !tempUnitMeasurement.matches ("ounces") || !tempUnitMeasurement.matches ("tablespoons") || !tempUnitMeasurement.matches ("teaspoons")){ new_ingredient.setUnitMeasurement(tempUnitMeasurement); System.out.println("Incorrect unit of measurement. Pleases enter valid unit of measurement"); tempUnitMeasurement =scnr.next(); }
//Prompt the user for calories per unit System.out.println("Please enter the calories per " + tempUnitMeasurement + ": "); tempNumberCaloriesPerUnit = scnr.nextInt();
//while loop to validate the calories input while(tempNumberCaloriesPerUnit < 1 || tempNumberCaloriesPerUnit > MAX_CALORIES){ System.out.println("Please enter correct number of calories per unit."); tempNumberCaloriesPerUnit =scnr.nextInt(); } new_ingredient.setNumberCaloriesPerUnit(tempNumberCaloriesPerUnit);
//compute total calories (Number cups X number calories per cup) tempTotalCalories = tempIngredientAmount * tempNumberCaloriesPerUnit;
//set ingredient attributes new_ingredient.setNameOfIngredient(tempNameOfIngredient); new_ingredient.setUnitMeasurement(tempUnitMeasurement); new_ingredient.setIngredientAmount(tempIngredientAmount); new_ingredient.setNumberCaloriesPerUnit(tempNumberCaloriesPerUnit); new_ingredient.setTotalCalories(tempTotalCalories);
//Display all the attributes System.out.println(new_ingredient.getNameOfIngredient() + "uses " + new_ingredient.getIngredientAmount() + " " + new_ingredient.getUnitMeasurement() + " and has " + new_ingredient.getTotalCalories() + " calories."); }
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