vendredi 24 février 2017

Java - Can't get if statement with Scanner to work [duplicate]

This question already has an answer here:

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