0

If I am entering the password which is not equal to the string "kiran", the program should keep on ask me to enter the correct password. But in the code that I am showing it is not doing that. Please correct my code as I believe that I am doing something wrong. Please correct me without using any other concept such as do-while.

public class WhilePrac {

    public static void main(String args[]) {

        String password;

        Scanner s = new Scanner(System.in);
        System.out.println("Enter Password");
        password = s.nextLine();

        while (password.matches("[^kiran]"))
        {

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

    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 2
    `if (!password.equals("kiran"))` – Pshemo Jun 03 '23 at 13:07
  • 1
    `matches` uses a regular expression and `[^abc]` (for example) would mean any ONE SINGLE letter with exception of `a`, `b`, or `c` - the ones listed after `^` - that is, it would match `"d"`, `"e"`, `" "`, `"0"`, ... but **not** `"a"`, `"b"`, or `"ef"` – user16320675 Jun 03 '23 at 13:09

0 Answers0