Results 1 to 7 of 7
  1. #1
    KevinC's Avatar
    KevinC is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Feel like a genius. Answer my pathetic looping question.

    Hey everyone. New to the forum. I want to get good at Java but it is painstaking so far.

    Here is my question.
    This is a tutorial for a command line application. I try to do all my exercises in Netbeans because I like GUIs.
    Could someone be kind enough to tell me why this loops endlessly, and possibly how to fix it?

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        
        // Generate a random number to be guessed
        int number = (int)(Math.random() * 101);
        
        // Get the integer from the textbox
        int guess = Integer.parseInt(userInput.getText());
                
        while (guess != number) {
              
        if (guess == number)
            answerOutput.setText("Yes, the number is " + number);
        else if (guess > number)
            answerOutput.setText("Your guess is too high");
        else
            answerOutput.setText("Your guess is too low");
        // End of loop     
        }
    Last edited by Fubarable; 01-14-2012 at 03:46 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Feel like a genius. Answer my pathetic looping question.

    Where do you change the value held by the guess variable inside of the loop?

    Oh, and welcome to the java-forums!

    In a minute, I'll add [code] [/code] tags to your code,... hang on!

  3. #3
    KevinC's Avatar
    KevinC is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Feel like a genius. Answer my pathetic looping question.

    I read you sentence about 10 times, and I am confused.

    I didn't think the value of guess would need to change...

    Perhaps I should come back after I do more research.

    I appreciate you helping me with this though.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Feel like a genius. Answer my pathetic looping question.

    Quote Originally Posted by KevinC View Post
    I read you sentence about 10 times, and I am confused.

    I didn't think the value of guess would need to change...
    Let's simplify. Say you have this bit of code:

    Java Code:
    int x = 0;
    
    while (x == 0) {
       System.out.println("x is 0 still");
    }
    Since x never changes inside of the loop, it has no chance of changing to a non-zero value, and the loop will never end. Contrast that with this loop:


    Java Code:
    int x = 0;
    
    while (x == 0) {
       System.out.println("x is 0 still");
       x = 3;
    }
    Now x changes inside of the loop to 3, and the loop can exit. So the sentinal variable (in this case, x) must change inside of the loop for the code's behavior to change (in this case, in order to exit the loop).

    Clear as mud?

  5. #5
    KevinC's Avatar
    KevinC is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Feel like a genius. Answer my pathetic looping question.

    Sweet. I got it.
    Thanks for your help good sir.
    I hate to be a bother with such trivial questions.

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        
        // Generate a random number to be guessed
        int number = (int)(Math.random() * 101);
        
        // Get the integer from the textbox
        int guess = -1;   
                
        while (guess != number) {
             guess = Integer.parseInt(userInput.getText());
        if (guess == number)
            answerOutput.setText("Yes, the number is " + number);
        else if (guess > number)
            answerOutput.setText("Your guess is too high");
        else
            answerOutput.setText("Your guess is too low");
        // End of loop     
        }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Feel like a genius. Answer my pathetic looping question.

    Quote Originally Posted by KevinC View Post
    Sweet. I got it.
    Thanks for your help good sir.
    I hate to be a bother with such trivial questions.

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        
        // Generate a random number to be guessed
        int number = (int)(Math.random() * 101);
        
        // Get the integer from the textbox
        int guess = -1;   
                
        while (guess != number) {
             guess = Integer.parseInt(userInput.getText());
        if (guess == number)
            answerOutput.setText("Yes, the number is " + number);
        else if (guess > number)
            answerOutput.setText("Your guess is too high");
        else
            answerOutput.setText("Your guess is too low");
        // End of loop     
        }
    You'll still never leave that loop because of a threading issue. Since the loop ties up the event thread, the GUI will become unresponsive leaving you no way to change the value inside of your JTextField.

  7. #7
    KevinC's Avatar
    KevinC is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Feel like a genius. Answer my pathetic looping question.

    I will re-visit your statement when I am more knowledgeable. I have a long journey ahead of me with this stuff.

Similar Threads

  1. Simple question! Please answer!
    By Asvin in forum New To Java
    Replies: 4
    Last Post: 11-09-2011, 02:53 AM
  2. A Rather Pathetic Question
    By anthropamorphic in forum New To Java
    Replies: 11
    Last Post: 07-06-2011, 08:57 PM
  3. please answer some basic question if you can
    By togaurav in forum New To Java
    Replies: 17
    Last Post: 04-16-2011, 09:23 AM
  4. Replies: 12
    Last Post: 07-27-2010, 10:40 AM
  5. Plz answer this question ...
    By raghu2114 in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 07:36 PM

Posting Permissions

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