-4

When I type the same password (jalalkay) it gives me "False" answer!

import java.util.Scanner;

public class password2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner pass = new Scanner(System.in);


        System.out.println("Type a password");
        String ps = pass.nextLine();

        if(ps == "jalalkay"){
            System.out.println("true");

        }else{
            System.out.println("false");
        }

    }

}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Jalal rka
  • 35
  • 1
  • 5

1 Answers1

3

You are using == to compare Strings. This is incorrect. Use .equals() instead.

if(ps.equals("jalalkay")){
    System.out.println("true");

The == operator checks for reference comparison (address comparison) and .equals() method checks for content comparison. To put it another way, == checks if both objects point to the same memory location and .equals() evaluates to the comparison of values in the objects.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45