Results 1 to 4 of 4
  1. #1
    bleh is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    bleh is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    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 04:35 AM.

  4. #4
    TheEnemy is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    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 04:17 PM.

Similar Threads

  1. New Command prompt
    By jmga9 in forum New To Java
    Replies: 4
    Last Post: 11-10-2010, 02:53 AM
  2. Replies: 4
    Last Post: 03-25-2010, 05:47 PM
  3. Jcreator to DOS Prompt
    By arshesander in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 07:25 AM
  4. help me!!!! about command prompt..
    By kureikougaiji in forum New To Java
    Replies: 2
    Last Post: 11-13-2008, 07:15 PM
  5. input prompt
    By angelbaby21 in forum New To Java
    Replies: 8
    Last Post: 08-25-2008, 05:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •