-1

I wanted to check if the password was entered correctly with Scanner in Java.

I tried with this code:

import java.util.Scanner;

public class test {

    public static void main(String[] args) {

        System.out.println("Please enter your username:");
        Scanner username = new Scanner(System.in);
        System.out.println("Hello " + username.nextLine() + ", please authenticate using your password:");
        Scanner password = new Scanner(System.in);
        String pw = "test";
        String pwEnter = password.nextLine();
        if (pw != pwEnter) {
            System.out.println("Sorry, you don't have access to the System.");
        } else {
            System.out.println("Thank you for authenticating, you can proceed with:");

        }
    }
}
sinclair
  • 2,812
  • 4
  • 24
  • 53
Sean
  • 41
  • 1
  • 5

1 Answers1

-1

Change

if (pw != pwEnter) {

to

if (! pw.equals(pwEnter) ) {

Take a look at this question: How do I compare strings in Java?

sinclair
  • 2,812
  • 4
  • 24
  • 53