This question already has an answer here:
- How do I compare strings in Java? 23 answers
I am trying to make a program that asks for the user's name, and prints "We have the same name" if the user enters "Charles". When I run it, I type in "Charles", but it prints "We don't have the same name". Any suggestions?
package testing;
import java.util.Scanner; public class testing {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String Name;
System.out.print("Enter your name: ");
Name = userInput.nextLine();
System.out.println(Name);
if(Name == "Charles"){
System.out.println("We have the same name!");
}
else{
System.out.println("We don't have the same name");
}
}
}
Aucun commentaire:
Enregistrer un commentaire