loop, int input and try/catch Exception probelm

Dear guys,

need some help.

below is part of my program,

first problem i need to loop if users input is not numeric(integer or double)

i know that i need a do while loop, but got problem in declaring the while statement.

also please see the questions indicated below at \\.....

for(......)

{

String quantityStr = JOptionPane.showInputDialog (null,"Enter part " +(index+1) +" quantity:");

try

{

Integer.parseInt(quantityStr);

}

catch (NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"Not an integer, please enter numeric values");

}

String priceStr = JOptionPane.showInputDialog (null,"Enter part " +(index+1) +" price per unit:");

try

{

Double.parseDouble(priceStr);

}

catch (NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"Not an integer, please enter numeric values");

}

quantity = Integer.parseInt(quantityStr);

price = Double.parseDouble(priceStr);

\\"sorry, if this question sounds stupid just learn java, why i have to"

\\"declare the 2 statement above again when it already been declare already, of course if i don't do it ,"

\\"it will show error on could not find quantity and price, i know that this is due to out of scope but is"

\\"there anyway i can put the 3 statement below within the scope?"

\\"if yes, where?"

inv[index] =new Invoice(quantity,price);

inv[index].setpartQuantity(quantity);

inv[index].setpriceUnit(price);

}

[2331 byte] By [htw_guya] at [2007-10-2 22:00:34]
# 1

This is one way to do it.

boolean validInput = false;

while(! validInput) {

get user input

if user input is valid {

validInput = true;

else

print error message

}

}

How you get and validate user input is upto you.

floundera at 2007-7-14 1:16:48 > top of Java-index,Java Essentials,New To Java...
# 2
thanks,But how do i put into statement ?:if user input is validfrom my program above. there is mutiple keys input from the keyboard which is not numeric.
htw_guya at 2007-7-14 1:16:48 > top of Java-index,Java Essentials,New To Java...
# 3
That is why I said the following.> How you get and validate user input is upto you.Think about how you are currently using the try/catch statement and how you can incorporate that into what I suggested.
floundera at 2007-7-14 1:16:48 > top of Java-index,Java Essentials,New To Java...
# 4

> > How you get and validate user input is upto you.

> Think about how you are currently using the try/catch

> statement and how you can incorporate that into what

> I suggested.

Sorry bro,

try it for the past hour, still stuck at

if user input is valid

i don't know how to reference it with the non numeric values.

anyway thanks

htw_guya at 2007-7-14 1:16:48 > top of Java-index,Java Essentials,New To Java...
# 5
.
htw_guya at 2007-7-14 1:16:48 > top of Java-index,Java Essentials,New To Java...
# 6

if the try works, validInput=true;

otherwise its false....

youll need a boolean for every string you read in adn your while will have to check for them all.

inside the while have an if around each try/catch that checks to see if input that was done was valid( ofcourse its false if its the first time)

that should work....

i hate JOptionPane...

PlasmaLinka at 2007-7-14 1:16:48 > top of Java-index,Java Essentials,New To Java...