+ 1
There is something wrong in this program(output).Always showing 'invalid user' output.
import java.util.Scanner; public class validuser { public static void main(String[] args){ System.out.println("Enter your name"); Scanner sc = new Scanner(System.in); String st = sc.nextLine(); String name = "KIRAN"; if(st == name){ System.out.println("Welcome"); } else{ System.out.println("Invalid user"); } } }
4 Antworten
+ 5
You're putting ==. == compares objects. Even if the strings are the same, theyre different string objects. do if(st.equals(name))
+ 2
hi, James. i thought the equals sign was exclusively for assigning values to variables.
+ 2
one = does. so x = 5 assigns 5 to x. == compares two objects and returns true if they are equal.
+ 2
This == sign you can used it with nubmbers int double..etc but string you should use ''example''.equals(''example'');