Maciaz
Maciaz

Reputation: 1224

Java, while loop with scanner inside and as a statement

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!");
}

Upvotes: 1

Views: 640

Answers (5)

VHS
VHS

Reputation: 10184

  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.

Upvotes: 2

Ella
Ella

Reputation: 1

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.

Upvotes: 0

J. Tate
J. Tate

Reputation: 163

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!");

}

Upvotes: 0

derOtterDieb
derOtterDieb

Reputation: 555

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!");

}

Upvotes: 0

Sweeper
Sweeper

Reputation: 271050

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!");
}

Upvotes: 0

Related Questions