I noticed a few things about my results from my codes.
If I don't key in anything,no matter what I press(the ok,cancel or the x button)
It will print out
Please enter an integer
value cannot be 0 or negative
and contiune to prompt me until the I key in a positive
value
If I type in a string value, it will also print out
Please enter an integer
value cannot be 0 or negative
and contiune to prompt me until the I key in a positive
value
what I want is
when I click on ok button
it will check whether the value is <=0 or is a string value.
if the value is a string value, it will print out
Please enter an integer
if the value is <=0, It prints out
value cannot be 0 or negative
when I click on cancel or the x button, it will close the JOptionPane
Please help.thanks
import javax.swing.JOptionPane;
public class OptionPaneTut {
public static void main(final String[] args) {
int value = 0;
boolean isPositive = false;
do {
try {
value = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter value?", null));
} catch (NumberFormatException e) {
System.out.println("Please enter an integer");
}
if(value <= 0) {
System.out.println("value cannot be 0 or negative");
}
else {
System.out.println("value is positive");
isPositive = true;
}
}while(!isPositive);
}
}