import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String n , p;
n = sc.next();
p = "Hi";
if(n == p){
System.out.println("Hi");
}else{
System.out.println("Bye");
}
}
}
Siya
Level 1
I am getting the output of this code as "Bye" but it should be "Hi"(input : Hi ), in taking input its coming wrong while if I am Initialising both n and p(as "Hi") in the code only then the output is coming as "Hi" , what's the error??
Resolved
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
mike3321
19 October 2022, 13:30
try comparing Strings by .equals() method insted of ==
0
Siya
19 October 2022, 13:31
can u tell in code
0
mike3321
19 October 2022, 13:33useful
+1
Siya
19 October 2022, 13:34
Let me check
0
Siya
19 October 2022, 13:36
Thanks , it is coming right , but what was the mistake before
0
mike3321
19 October 2022, 13:41useful
You should always compare Strings using .equals, not == operator
+1
Siya
19 October 2022, 13:42
Ok thanks
+1