I have heavily commented my code to assist both myself and anyone else helping me in determining what went wrong.
I am trying to make a loop that only repeats if the user inputs inappropriate data such as letters or negative numbers. I do not understand why my boolean will not work in this case. My code runs just fine when catching letters, but it will not repeat the prompt for new data. I have tried using a do/while loop, as well as a regular while loop. The boolean value almost seems to work backwards. Thank you in advance for your time in helping me.
import java.util.*;
public class Main
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
//initial declerations
double feet;
double inches;
double centimeters;
double totalInches;
boolean end = false;// IN THEORY becomes true when no exeption occurs
// BOOLEAN DOES NOT WORK HERE FOR WHAT I WANT IT TO DO
while(end = false)
{
//bring in data
try
{
System.out.println("Please enter number of feet: ");
feet = console.nextDouble();
System.out.println("Please enter number of inches: ");
inches = console.nextDouble();
//convert from feet and inches to just inches
totalInches = inches + (feet * 12);
//convert from totalInches to centimeters
centimeters = (totalInches * 2.54);
//print output
System.out.println("The length of " + feet + " feet " + inches + " inches");
System.out.println("is equivilent to: " + centimeters + "centimeters");
end = true;// WHY WON'T THIS WORK ?
}
catch (InputMismatchException imeRef)//catch non-numerical input
{
System.out.println("Exception " + imeRef);
}
}//end of while loop
}//end of main()
}//end of class

New Topic/Question
Reply




MultiQuote








|