Java : scanner input comparison with string

`

My java code is :

canner sc = new Scanner(System.in) ;
String y = "" ;
String z = "a" ;

System.out.print("Type a number : ");
y = sc.next();

System.out.println( y + " == " +z+ " :");
System.out.println( y == z);
The result printout :

Type a number : a

a == a : false

However, my expectation is TRUE.

Yes, it seems so. But in Java You cannot use == for comparing strings, You should use equals
Scanner sc = new Scanner(System.in) ;
System.out.println("Type a character a : ");
String y = sc.nextLine();
String z = “a” ;

System.out.println( y + " == " +z+ " :");
System.out.println( y.equalsIgnoreCase(z));

made small changes in Your code. To get message out before You should input your character

1 Like

Thanks for your Hints.