This question already has an answer here:
- How do I compare strings in Java? 23 answers
Why does this output "Fail" and not "Success" when I type in "test"? It works when I just set the "word" variable in the code, but if I set it based on user input, it doesn't work.
import java.util.Scanner;
public class ifTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String word = input.nextLine();
if (word == "test") {
System.out.println("Success");
}
else {
System.out.println("Fail");
}
}
}
Aucun commentaire:
Enregistrer un commentaire