exception handling

this code is the driver code for a program i have been working on, the only problem i have left with it is that with the exception handling. when a string is enteres in either the quantity or price the warning come up but then it goes straight to the nex dialog box instead of repeating the previous question. how can i fix the exception handling so it reverts back to the question it asked when the incorrect value was entered.

publicstaticvoid main(String[] args)

{

String input;

double number;

double invoiceTotal = 0;

Invoice myInvoice[] =new Invoice[3];

for (int index = 0; index < 3; index++)

{

double num = 0;

myInvoice[index] =new Invoice();

input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) +" Number");

myInvoice[index].setPartNumber(input);

input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) +" Description");

myInvoice[index].setDescription(input);

do

{

input = JOptionPane.showInputDialog("Please Enter Quantity of Part " + (index + 1) +" Required");

try

{

number = Double.parseDouble(input);

myInvoice[index].setQuantity(number);

}

catch (NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"You must enter an double");

}

}

while(input=="");//ENDDOWHILELOOP

do

{

input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) +" Price");

try

{

number = Double.parseDouble(input);

myInvoice[index].setPrice(number);

}

catch (NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"You must enter an double");

}

}

while(input=="");

}

invoiceTotal = myInvoice[0].getTotal() + myInvoice[1].getTotal() + myInvoice[2].getTotal();

JOptionPane.showMessageDialog(null,"Part " +"Description " +"Qty " +"Price\n" +

" " + myInvoice[0].getPartNumber() +" " + myInvoice[0].getDescription() +" " +

myInvoice[0].getQuantity() +" " + myInvoice[0].getPrice() +"\n" + myInvoice[1].getPartNumber() +" " + myInvoice[1].getDescription() +" " +

myInvoice[1].getQuantity() +" " + myInvoice[1].getPrice() +"\n" + myInvoice[2].getPartNumber() +" " + myInvoice[2].getDescription() +" " +

myInvoice[2].getQuantity() +" " + myInvoice[2].getPrice() +"\n" +"$" + invoiceTotal);

}

}

[4295 byte] By [sorazia] at [2007-11-27 4:44:09]
# 1

Try breaking you code into smaller chunks and put these into separate methods. Rough example:

while user input is invalid {

get user input

validate user input

}

private boolean validateUserInput(user input) {

try {

I assume you want to parseInt

return true // input must be valid to reach this statement

} catch Exception {

print error // or whatever

}

return false // input must be invalid to reach this statement

}

floundera at 2007-7-12 9:56:03 > top of Java-index,Java Essentials,New To Java...
# 2
I cant seem to get that to work either
sorazia at 2007-7-12 9:56:03 > top of Java-index,Java Essentials,New To Java...
# 3
And what are we supposed to do? Guess what "doesn't work" means or guess what you have tried.
floundera at 2007-7-12 9:56:03 > top of Java-index,Java Essentials,New To Java...
# 4
I tried to incorporate your example into my driver program but was unable to succesfully code it correctly.
sorazia at 2007-7-12 9:56:03 > top of Java-index,Java Essentials,New To Java...
# 5
Well that clears everything up.In case I was too subtle for you: post your updated code so we don't have to guess what the hell you are trying to do.
floundera at 2007-7-12 9:56:03 > top of Java-index,Java Essentials,New To Java...