1

The program asks for password and you enter it, else you get 3 attempts and if you fail 3 times it locks (in theory).

Questions:

  1. How should I write the while loop, now it just says "Line can't be resolved to a variable" and I don't know how to solve this.

  2. The subtraction of attempts also doesn't work. How should I write it?

Here's the code:

import java.util.Scanner;

public class Application {
    public static void main(String[] args) {

        while(line != correctPass) {

            String correctPass = "Java";

            System.out.println("Enter the password"); 
            Scanner input = new Scanner(System.in);
            String line = input.nextLine();

            if(line.equals(correctPass)) {
                System.out.println("Wellcome back sir!");
                break;
            }
            else {
                int num = 3;
                System.out.println("Wrong password, please try again! "  + num + " attempts left!");
                num = num - 1;
                    if(num == 0) {
                        System.out.println("System Locked!");
                        break;
                    }
            }
        }
    }
}
Jeremy Barnes
  • 657
  • 4
  • 15
  • 1
    Instead of saying "now it just says "Line can't be resolved to a variable" ", please pose the error code given by the compiler in your question. – Captain Man May 14 '15 at 19:47

5 Answers5

2

line is used before it is declared. Try putting the declaration at the top as String line = null.

Also, because you are setting the num = 3 in the loop, it never gets decremented. It needs to be set to 3 outside of the loop.

I would seriously suggest checking out this tutorial.

There are some other issues too, you may want to check out the difference between something != somethingElse and !something.equals(somethingElse).

Captain Man
  • 6,997
  • 6
  • 48
  • 74
1

The variables line and correctPass is declared inside the loop, hence the condition statement will not have access to those variables, it should be declared and initialized outside the loop.

It could be something like this:

public class Application {
    public static void main(String[] args) {
        String correctPass = "Java";
        Scanner input = new Scanner(System.in);
        int num = 3;

        System.out.println("Enter the password"); 

        String line;
        while (num != 0 && !(line = input.nextLine()).equals(correctPass)) {

            System.out.println("Wrong password, please try again! "  + num + " attempts left!");
            num = num - 1;
            System.out.println("Enter the password");
        }

        if (num == 0) {
            System.out.println("System Locked!");
        } else {
            System.out.println("Wellcome back sir!");
        }
    }
}
MJSG
  • 1,035
  • 8
  • 12
  • Yours actually works. Thank you. I will look for my mistakes and correct them. –  May 14 '15 at 19:59
1

I would prefer to use do while which is the actual logic of do while that you're trying to achieve in a while.

import java.util.Scanner;

public class Application { 

    public static void main(String[] args) {

        String    correctPass = "Java";
        int          attempts = 3;
        boolean authenticated = false;

        Scanner         input = new Scanner(System.in);

        do {
            System.out.println("Enter the password");
            String   userPass = input.nextLine();
            if( userPass.equals(correctPass) ){
                System.out.println("Welcome back sir!");
                authenticated = true;
            } else {
                System.out.println("Wrong password, please try again! "  + attempts + " attempts left!");
                attempts = attempts - 1;
                if(attempts == 0) {
                    System.out.println("System Locked!");
                    break;
                }
            }
        } while(! authenticated); 

        input.close();
    }
}

To point the mistakes in your code,

1) You need to use .equals() to compare a String

2) The original password should be declared before the loop

3) You're re-initialising num variable for every loop (so definitely you won't be locked at any situation ever), so it should also be declared before the loop.

The Coder
  • 2,562
  • 5
  • 33
  • 62
  • Thats a bit too much for me. Im just starting out and i finished the UserInput course on udemy, so this got on my mind and i decided to make it. –  May 14 '15 at 20:04
  • I actually like this answer better, I sort of always forget that do-whiles are a thing. – Jeremy Barnes May 14 '15 at 20:17
0

You need to declare line outside the while loop - variables don't exist (and can't be referenced) before they are declared.

You use both correctPass and line in the while loop condition, but they aren't created until 1 and 3 lines later, respectively.

 while(line != correctPass) {
    String correctPass = "Java";

    System.out.println("Enter the password"); 
    Scanner input = new Scanner(System.in);
    String line = input.nextLine();

You need to reformat it to look more like this:

String correctPass = "Java";
int num = 3;
Scanner input = new Scanner(System.in);
System.out.println("Enter the password");
String line = input.nextLine();

while(!line.equals(correctPass)) {
       num = num - 1;
       if(num == 0) {
            System.out.println("System Locked!");
            break;
        }
       System.out.println("Wrong password, please try again! "  + num + " attempts left!");
       line = input.nextLine();
}

Why:

There's no need to recreate correctPass each time the while loop runs, since its the same every time.

Similarly, its silly to recreate the scanner each time, since it isn't changing.

And, as pointed out in comments, if you define num each time you loop it will never reach zero and therefore never lock, since it is redefined as 3 each time.

.equals is needed to compare strings. This is why if you're curious.

Community
  • 1
  • 1
Jeremy Barnes
  • 657
  • 4
  • 15
  • 1
    `num` is re-defined in the while loop, which means "The system will never be locked" – The Coder May 14 '15 at 19:45
  • Domagoj - don't declare it inside the loop, declare it once outside, and set the value inside. Declaring is where you first create the variable (`String line;`) and setting the value is any time you put a new value in the variable (`line = "some string";`) – Jeremy Barnes May 14 '15 at 19:49
  • Ok, the good thing is that i understand what i did wrong. The bad thing is that now when i write the wrong password it just displays "wrong pass etc" 3 times and locks. I wrote the code just like you did. –  May 14 '15 at 19:56
  • Right, you need to reset line to whatever comes in from the scanner again. – Jeremy Barnes May 14 '15 at 19:58
  • The same way you did earlier - `line = input.nextLine();` – Jeremy Barnes May 14 '15 at 20:06
0
  1. You the String variable is initialised AFTER your while statement, so line does not exist at the moment your first while condition is evaluated
Deb
  • 1,098
  • 10
  • 22