I am trying to establish a loop that handles exceptions while receiving input from the user. I need to get 5 values and I need to have them repeat the input if an error is caught. I am using a while loop to do this, and for the most part it works. I have a problem. If I enter the first value in incorrect format it will tell me the error and allow a re-enter. The problem is when I enter the first value correctly, and then an erroneous second, third, fourth value, it will skip right through to the end of the program without letting me finish entering the values. My error is somewhere in this block of code:
while(!validInput)
{
try
{
for(item = 0 ; item < priceArray.length; item )
{
System.out.println("Enter a price: ");
inputPrice = Double.parseDouble(stdin.readLine());
priceArray[item] = inputPrice;
validInput = true;
}
}
catch(IOException re)
{
System.out.println(re.getMessage() "There was an IO exception.");
}
catch(NumberFormatException ne)
{
System.out.println( ne.getMessage()
" is not a valid format for an integer." );
}
}
Tags: