1

Trying to run the while loop for a password check. I do keep getting an error that the variable haslo cannot be found. Tried declaring it outside the loop - then it says, it's already declared. I know it can be done with infinite loop and the break command. Just curious whether it's possible this way.

String password = "pw123";

while (!haslo.equals(password)){

        System.out.println("Enter pw:");
        String haslo = reader.nextLine();

        if (haslo.equals(password))
            System.out.println("Right!");

        else
            System.out.println("Wrong!");
}
3
  • 1
    "Tried declaring it outside the loop" <- That was the right way. Declare it once, then just update the value inside the loop: haslo = reader.nextLine(); Commented Sep 18, 2017 at 15:46
  • cos you are trying to read the value of haslo before it is declared... Commented Sep 18, 2017 at 15:46
  • side bar: the loop must be executed at least once. Consider changing to a do-while loop. Commented Sep 18, 2017 at 15:53

5 Answers 5

2
  1. Declare the variable haslo as String haslo = ""; before the loop starts.
  2. In the loop, replace your line String haslo = reader.nextLine(); with haslo = reader.nextLine();.

Reasoning:

For 1 --> Your while loop is referencing the variable haslo before it is declared. So you need to declare it before it is referenced.

For 2 --> Once it is declared, you don't want to re-declare it because the one declared before the loop is already made available within the scope of the loop.

Sign up to request clarification or add additional context in comments.

Comments

0

This is all about scopes.

Your haslo variable is declared inside the while loop and so can only be used inside it, after he declaration. The condition of the while loop is evaluated before the declaration of haslo so you can't use haslo there.

To fix this, declare haslo outside the while loop:

String haslo = "";
while (!haslo.equals(password)){

    System.out.println("Enter pw:");
    haslo = reader.nextLine(); // <-- note that I did not write "String" here because that will declare *another* variable called haslo.

    if (haslo.equals(password))
        System.out.println("Right!");

    else
        System.out.println("Wrong!");
}

Comments

0

Can you try something like this ? You need to declare haslo before the loop, then you can use it inside the loop, and avoid the infinite loop.

String password = "pw123";
String haslo = "";

while (!haslo.equals(password)){

    System.out.println("Enter pw:");
    haslo = reader.nextLine();

    if (haslo.equals(password))
        System.out.println("Right!");

    else
        System.out.println("Wrong!");

}

Comments

0

The problem can be found by following it step by step:

String password = "pw123"; (Good)

while (!haslo.equals(password)){ (Bad. What is Haslo? It is not defined before you use it here)

Try this:

String password = "pw123";
String haslo = "";
while (!haslo.equals(password)){
    System.out.println("Enter pw:");
    haslo = reader.nextLine();

    if (haslo.equals(password))
        System.out.println("Right!");

    else
        System.out.println("Wrong!");

}

Comments

0

Declare String INSIDE loop again! otherwise next time loop will not work! you gave it a reference, but loop needs to know what the reference is again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.