0

I'm trying to make the incorrect password start the loop over until the right password is inputted (which it does) but System.out.println statements repeat infinitely in the console. A break statement just terminates the code, but I want it to keep going except only printing the statements once. Any solutions?

    public static void main(String[] args){

    System.out.println("What is the password?");

    Scanner passwordScan = new Scanner(System.in);
    String pass = passwordScan.nextLine();
    String password = "JohnnyRoberts123";


    while(password != pass) {
        System.out.println("Wrong password!");
        System.out.println("Try again, what is the password?");

        continue;
  • 1
    Since you never modify either `password` or `pass` the condition will always evaluate to the same thing - so you should obviously change that – UnholySheep May 28 '20 at 21:32
  • provide us with the password input method please – Tarek AS May 28 '20 at 21:33
  • I clarified them beforehand @UnholySheep – Jordan Molina May 28 '20 at 21:33
  • You got the input *once* - so at what point would you expect your loop condition to evaluate to false? You need to get input again for it to change – UnholySheep May 28 '20 at 21:37
  • Also you are comparing the strings wrong, see: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – UnholySheep May 28 '20 at 21:38
  • @JordanMolina - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 30 '20 at 10:13

4 Answers4

0

I don't see any code that would get any input from the user. The while loop gets immediately reiterated when it hits continue.

You rather want something like:

java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(password != pass) {
        System.out.println("Wrong password!");
        System.out.println("Try again, what is the password?");
        password = in.readLine();
        continue;
}

px1mp
  • 5,212
  • 1
  • 18
  • 10
  • You have done the same serious mistake that OP has done. You should never compare strings using `!=` as `!=` is used to compare references, not the content. – Arvind Kumar Avinash May 28 '20 at 21:45
0

First, never hardcode the password. If just for testing , it's ok.

Second, you must update the password in the while loop, in order to check for validation as follows :

public static void main(String[] args){

    System.out.println("What is the password?");

    Scanner passwordScan = new Scanner(System.in);
    String password = "JohnnyRoberts123";
    String pass="";

    while(!password.equals(pass)) {
        System.out.println("Wrong password!");
        System.out.println("Try again, what is the password?");
        pass = passwordScan.nextLine();
        continue;
}

Notice here, you can't compare strings as integers using == , Strings are treated as objects so you must compare them by using str1.equals(str2)

Tarek AS
  • 187
  • 13
0

You just need to move your code around a little so the line asking for input (passwordScan.nextLine()) is inside the while loop:

public static void main(String[] args) {
    Scanner passwordScan = new Scanner(System.in);
    String password = "JohnnyRoberts123";
    System.out.println("What is the password?");

    while (true) {
        String pass = passwordScan.nextLine();

        if (!password.equals(pass)) {
            System.out.println("Wrong password!");
            System.out.println("Try again, what is the password?");
        } else {
            break;
        }
    }
}

Also note that you should never use != to compare strings, always use the equals method because it actually compares the string contents, not just the objects themselves.

Charlie Armstrong
  • 2,332
  • 3
  • 13
  • 25
0

There are two problems with your code:

  1. You have used != to compare strings, which is not correct. Note that == or != compares the references, not the content.
  2. You haven't placed the input part in the while loop so that in case of wrong input, the user can be asked to enter the password again.

Do it as follows:

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        Scanner passwordScan = new Scanner(System.in);
        String pass = "";
        String password = "JohnnyRoberts123";

        while (!password.equals(pass)) {
            System.out.println("What is the password?");
            pass = passwordScan.nextLine();
            if (!password.equals(pass)) {
                System.out.println("Wrong password!");
                System.out.println("Try again, what is the password?");
                continue;
            }
        }
        // ...rest of code
    }
}

A sample run:

What is the password?
hello
Wrong password!
Try again, what is the password?
What is the password?
hi
Wrong password!
Try again, what is the password?
What is the password?
JohnnyRoberts123
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110