Results 1 to 4 of 4
Thread: RE: JOptionPane Prompt
- 02-13-2011, 02:42 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
RE: JOptionPane Prompt
Hello, I had a quick question for some of you experts out there.
I have an assignment where I have to set up a loop that sets it so that you cannot input a negative number
if (x < 0)
{
JOptionPane.showMessageDialog(null, "Cannot be negative, try again");
JOptionPane.showInputDialog("How tall are you");
}
When I put two negative numbers in a row, it just moves on. However, when I change the if-statement to a while-loop, the executed application never closes, even when I put in a positive number. How can I turn this into something that asks the question more than once, if I keep inputting a negative number?
Thanks
- 02-13-2011, 03:20 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
when I change the if-statement to a while-loop, the executed application never closes, even when I put in a positive number.
It is not enough to just ask the question inside the loop: you also have to set the value of x so that the updated value will be checked.
If have problems doing this post the code you are using: just a brief example with a main() method that prompts until it gets a valid response.
- 02-13-2011, 03:32 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
I apologize, I do not understand what you are saying. Could you provide an example?
edit: maybe this might help..
String height = JOptionPane.showInputDialog("How tall are you");
int x = Integer.parseInt(height);
while (x < 0)
{
JOptionPane.showMessageDialog(null, "Cannot be negative, try again");
JOptionPane.showInputDialog("How tall are you");
}Last edited by bleh; 02-13-2011 at 03:35 AM.
- 02-13-2011, 02:54 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
As pbrockway2 said, the issue is that your while loop never update height after the user enters it the second time, so it'll loop forever with the negative number entered the first time. This small change should make sure that as soon as you enter a non-negative number, the loop will stop.
Java Code:String height = JOptionPane.showInputDialog("How tall are you"); int x = Integer.parseInt(height); while (x < 0) { JOptionPane.showMessageDialog(null, "Cannot be negative, try again"); height = JOptionPane.showInputDialog("How tall are you"); x = Integer.parseInt(height); }
Also note that if you enter something else than a number (letter, special characters, etc.) your parseInt call will throw a NumberFormatException.Last edited by TheEnemy; 02-13-2011 at 03:17 PM.
Similar Threads
-
New Command prompt
By jmga9 in forum New To JavaReplies: 4Last Post: 11-10-2010, 01:53 AM -
How to cause a second prompt to run after the first.
By G-Unit in forum New To JavaReplies: 4Last Post: 03-25-2010, 04:47 PM -
Jcreator to DOS Prompt
By arshesander in forum New To JavaReplies: 5Last Post: 02-17-2010, 06:25 AM -
help me!!!! about command prompt..
By kureikougaiji in forum New To JavaReplies: 2Last Post: 11-13-2008, 06:15 PM -
input prompt
By angelbaby21 in forum New To JavaReplies: 8Last Post: 08-25-2008, 04:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks