This is my first post and I'm a newly enrolled student/novice to Java.
I have created a simple program that calculates the area of a box, and the error handling I have obviously got wrong. When I run the program, it asks the first question ("Enter the height of the box") and then handles the error ok when invalid data entered, THEN when asked the second question ("Enter the width of the box") it handles the error BUT then takes you all the way back to the first question instead of returning to the second.
Can someone please help out here as I'm sure it would be a simple fix.
I have posted the code below.
Thanks - Stew.
import java.io.*;
public class TestProg
{
public static void main(String args []) throws IOException
{
String strHeight, strWidth;
float height, width, total;
boolean done = false;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
while(!done)
{
try
{
System.out.print("Enter the height of the box: ");
strHeight = dataIn.readLine();
height = Float.parseFloat(strHeight);
System.out.println();
if(height <= 0) throw new NumberFormatException();
System.out.print("Enter the width of the box: ");
strWidth = dataIn.readLine();
width = Float.parseFloat(strWidth);
System.out.println();
if(width <= 0) throw new NumberFormatException();
else done = true;
total = height * width;
System.out.println("The area = " + total);
}
catch(NumberFormatException e)
{
System.out.println("Invalid format!");
}
}
}
}

New Topic/Question
Reply




MultiQuote






|