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 8 of 8

Thread: Simple question re while loop and input

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple question re while loop and input

    Hi guys, I'm stuck on the following simple problem. Method 1 asks for input. Method 2 gets input and checks it against a criterion - if it fails, we start back at method 1, if it passes, we finish. But it doesn't work - if the input fails the test the first time around, it gets stuck in a loop where the new input isn't recognised. I'm really struggling with understanding why/how this is happening, and I'd be eternally grateful if somebody could help me.

    As an example, if the user inputs a value less than 10, the program goes back to Method 1, BUT, if they then enter a (valid) number greater than 10 the system still goes back to Method 1 - it doesn't exit. I suspect it has something to do with using the while loop - I understand I can use an if statement instead, but I want to understand why it fails with the while loop. Any feedback appreciated.

    Cheers,

    Al.
    import java.util.Scanner;
     
    public class TESTINGCODE {
     
        public static void main(String[] args) {
            Method1();}
     
        public static void Method1(){
            System.out.println("Enter an integer greater than 10");
            Method2();}
     
        public static void Method2(){
            Scanner input = new Scanner(System.in);
            int value = input.nextInt();
     
            while(value < 10){
                Method1();
            }
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple question re while loop and input

    Try debugging the code by adding println statements to print out the values of the variables as the code executes. If you can see what the computer sees when it executes the code, it should help you understand what the the program is doing and why it is doing it.

    Consider that value is a local variable in the Method2() method. Every call to Method2() creates a new instance of value. To exit the loop you need to change the contents of the value variable.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple question re while loop and input

    Quote Originally Posted by Norm View Post
    Try debugging the code by adding println statements to print out the values of the variables as the code executes. If you can see what the computer sees when it executes the code, it should help you understand what the the program is doing and why it is doing it.

    Consider that value is a local variable in the Method2() method. Every call to Method2() creates a new instance of value. To exit the loop you need to change the contents of the value variable.
    Thanks Norm, I'd already done exactly what you proposed but left it out for simplicity's sake. Although you'd expect it to help with my understanding, it did the opposite. I put in four println statements where value was printed to the screen and when in the erroneous loop the code seemed to be running in reverse order). Suffice it to say, it didn't help with understanding the issue. I feel the issue relates to the use of the while loop which seems to be looping onto itself - it all works perfectly if I substitute an if statement.

    Doesn't the assignment of an integer to the value variable overwrite any existing value in the variable as opposed to creating another instance?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple question re while loop and input

    assignment of an integer to the value variable overwrite any existing value in the variable
    Yes, a variable only holds one value, the last one that was assigned to it.

    Did you understand this: value is a local variable in the Method2() method. Every call to Method2() creates a new instance of value.

    the code seemed to be running in reverse order
    Can you post the code that did that and also what was printed out?
    I don't understand what "reverse order" means.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple question re while loop and input

    Thanks for the feedback mate.

    Yes, I understand that value is a Method2() local variable and the implications re scope. Would that have an effect in this case, as Method1 one doesn't reference it (so value's scope is sufficient?).

    I've posted slightly updated code with 4 println statements which include a letter, A, B, C or D depending upon their position in the code. The reverse order refers to the fact that during an "erroneous" loop where the code is behaving strangely, the statements are printed in the following order ADCB - in some situations, this would be perfectly normal, but in this one I'm completely unable to understand how it's happening. For example, the "D" println statement, which is the very last line of code is executed and then, instead of the system exiting, it continues executing. I've also posted my interactions with the code below.
    import java.util.Scanner;
     
    public class TESTINGCODE {
     
        public static void main(String[] args) {
            Method1();}
     
        public static void Method1(){
            System.out.println("Enter an integer greater than 10");
            Method2();}
     
        public static void Method2(){
            Scanner input = new Scanner(System.in);
     
            int value = input.nextInt();
     
            System.out.println("A - value is now: "+ value);
     
            while(value < 10)
            {
                System.out.println("B - value is now: "+ value);
                Method1();
                System.out.println("C - value is now: "+ value);
            }
            System.out.println("D - value is now: "+ value);
     
        }
    }

    commentary window interactions using the above code:

    run:
    Enter an integer greater than 10
    1
    A - value is now: 1
    B - value is now: 1
    Enter an integer greater than 10
    100
    A - value is now: 100
    D - value is now: 100
    C - value is now: 1
    B - value is now: 1
    Enter an integer greater than 10
    100
    A - value is now: 100
    D - value is now: 100
    C - value is now: 1
    B - value is now: 1
    Enter an integer greater than 10

    [And so on]

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple question re while loop and input

    Notice value is always the same for the B and C messages. Once inside the loop, value never changes which means the loop will go forever.

    Here's a technique that will make for easier and faster testing. Notice where the Scanner object is defined.
    import java.util.Scanner;
     
    public class TestingCode {
        static  Scanner input = new Scanner(" 1 2 10 11 12 13 14"); //System.in);
     
     
        public static void main(String[] args) {
            Method1();
       }             //<<<<<<<<<<<<<< was Hidden ??????????????
     
        public static void Method1(){
            System.out.println("Enter an integer greater than 10");
            Method2();
            System.out.println("back from M2, exiting M1");
        }
     
        public static void Method2(){
     //       Scanner input = new Scanner(System.in);
            int value = input.nextInt();
            System.out.println("read val="+value);
     
            while(value < 10){
                Method1();
                System.out.println("back from M1 value=" + value);
            }
            System.out.println("exiting M2 value="+value);
        }
    }

    That definition allows you to preload ALL the value to be read by the Scanner class's methods.
    Makes for much faster testing.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple question re while loop and input

    Thanks for the tip re Scanner use, and yeah you're right about the value not being updated. I just don't understand why. If Method1 is called, shouldn't the value variable be updated when new input is given to it? I'm unsure if I'm missing something incredibly obvious, or if it's something obscure in the way the language is put together. Anyway, I'll keep playing around with it.

    Cheers,

    Al.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple question re while loop and input

    value not being updated. I just don't understand why.
    EVERY TIME Method2() is called NEW variables are created. The contents of the old variables are saved on the stack when Method1() is called and are not available until the code returns from Method1().
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. User Input with a Do Loop
    By RadiantChaos in forum Loops & Control Statements
    Replies: 4
    Last Post: March 13th, 2012, 07:14 PM
  2. input controlled loop
    By pezbirdy in forum Loops & Control Statements
    Replies: 4
    Last Post: November 10th, 2011, 06:16 AM
  3. Simple Input Dialog Question
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: March 1st, 2010, 11:10 PM
  4. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM