Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

While loop works only once.

Hi Guys! I'm trying to validate user input with while function and a method calling another method.

public void takeUserChoice(){
        String input = scanner.nextLine();
        this.userChoice = Integer.parseInt(input);
        System.out.println("userChoice in takeUserChoice is " + userChoice);
        validateUserChoice(userChoice);
    }

    public void validateUserChoice(int i){
        while (i < 1 || i > 6) {
        System.out.println("userChoice in normalize is " + userChoice );
        System.out.println("Please enter a number from 1 to 5.");
        takeUserChoice();
        }
    }   

It works only once when a value ranging from 1 to 5 is entered. But when different one is entered it keeps on asking for input forever, even after proper value is entered later.

I've changed validateUserChoice to this one below and called it from the mains and surprisingly enough it works. Any ideas? :)

//main 
game.giveChoice();
        game.takeUserChoice();
        game.validateUserChoice();
//end of main

public void takeUserChoice(){
        String input = scanner.nextLine();
        this.userChoice = Integer.parseInt(input);
        System.out.println("userChoice in takeUserChoice is " + userChoice);
        validateUserChoice();
    }

    public void validateUserChoice(){
        while (this.userChoice< 1 || this.userChoice > 6) {
        System.out.println("userChoice in normalize is " + userChoice );
        System.out.println("Please enter a number from 1 to 5.");
        takeUserChoice();
       }

1 Answer

It seems this may be a good time to look into the difference between pass by reference and pass by value. https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

In your first shared example, when you run takeUserChoice() and call validateUserChoice(int i), you pass the value of i to validateUserChoice(). if the value passed doesn't meet the while condition, then no code is run and validateUserChoice() ends. all is well.

However, if the passed in value is either less than 1 or greater than six, it executes the code, which in turn calls take user choice again. However, the original passed in value of take user choice hasn't changed. Whatever you passed in is what the while loop will be checking ad infinitum, because all it has to reference is the passed in value, not the class property of userChoice.

In the second shared example, you are checking against the user choice property of the instantiated class. This will check against the class property at the time the code is executed, giving you the expected result.

I think. ;)

Joseph, thanks for spending your time to answer! I'll definitely revisit passed by value subject :)

The funny thing is that I've included some println statements in both methods to see what is the value of the passed argument. Pls see below for print out. And as you can see, passed value changes with the input. Programming can be mysterious ;)

What is your choice? Type choosen number.

9

userChoice in takeUserChoice is 9

userChoice in validate is 9

Please enter a number from 1 to 5.

5

userChoice in takeUserChoice is 5

userChoice in validate is 5

Please enter a number from 1 to 5.

0

userChoice in takeUserChoice is 0

userChoice in validate is 0

Please enter a number from 1 to 5.