samedi 30 janvier 2016

Java if else statement doesn't work with Scanner [duplicate]

This question already has an answer here:

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