Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Loop error

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Loop error

    I'm trying to write a loop program prompts the user to enter a number. The number has to be between 10 and 20 (both inclusive). If the number input is not between that range, it will display an error message and prompts the user to re-enter a new number again until the user get it right.

    When I enter any number between 10 and 20 it works, but when I input in a number out of the range nothing happens. Anyone know what is wrong with my code??

    import javax.swing.JOptionPane;
    public class input {
        public static void main(String[] args){
     
            int number;
            String input = JOptionPane.showInputDialog(null,"Enter enter a number",JOptionPane.INFORMATION_MESSAGE);
            number = Integer.parseInt(input);
     
            while ((number>=10)&&(number<=20))   
             if   ((number>=10)&&(number<=20))
             {   JOptionPane.showMessageDialog(null,"End of program!"); break; }  
     
              else 
        {JOptionPane.showMessageDialog(null,"Pls enter a number between 10 and 20","Error",JOptionPane.ERROR_MESSAGE); break;}
     
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,364
    My Mood
    Mellow
    Thanks
    143
    Thanked 598 Times in 585 Posts

    Default Re: Loop error

    Think about how you'd do it in normal conversation:

    You: Pick a number
    Me: 5
    You: That's too low. Pick a number.
    Me: 30
    You: That's too high. Pick a number.
    Me: 15
    You: Ahhh. That's just right.

    From that, you can see that "Pick a number" is repeated, the answer is received and evaluated, and a response is generated. If the response is evaluated as out of bounds, the "Pick a number" prompt is repeated. That repetition is what must occur inside the loop. In pseudo code:
    theAnswerIsOutOfBounds = true
    do
    {
        // pick a number
     
        // evaluate the number and provide feedback
     
        // if the number is okay, theAnswerIsOutOfBounds = false
     
    } while ( theAnswerIsOutOfBounds )

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    skypi205 (November 3rd, 2013)

Similar Threads

  1. Loop & custom exception error Please help
    By dpjmie in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 18th, 2013, 04:40 PM
  2. [SOLVED] error with equals in arraylist loop
    By Randor in forum Loops & Control Statements
    Replies: 2
    Last Post: November 27th, 2012, 09:46 AM
  3. [SOLVED] Simple Increment in a Loop error
    By chrisob in forum Loops & Control Statements
    Replies: 7
    Last Post: May 22nd, 2012, 07:33 AM
  4. Possible Loop Error...
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 5th, 2010, 02:17 PM
  5. Error when loop used
    By anandkokatnur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 24th, 2010, 11:17 AM