I have a program where i ask a user for numberOfCups for and ingredient, i want to make sure that the user input is of type int and only int how can i ensure they do so?
String nameOfIngredient;
double numberOfCups;
int numberCaloriesPerCup;
double totalCalories;
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter the name of the ingredient: ");
nameOfIngredient = scnr.nextLine(); // using nextLine incase ingredient is more than one word long
System.out.println("Please enter the number of cups of " + nameOfIngredient + " we'll need: ");
numberOfCups = scnr.nextDouble(); // this variable is defined as double but it was written as nextFloat so i changed it
System.out.println("Please enter the number of calories per cup: ");
numberCaloriesPerCup = scnr.nextInt();
totalCalories = numberOfCups * numberCaloriesPerCup ;
System.out.println(nameOfIngredient + " uses " + numberOfCups + " cups and has " + totalCalories + " calories.");
}
}