mardi 5 janvier 2016

If statment in java isn't working [duplicate]

This question already has an answer here:

I am trying to make a simple java program, but part of it is not working!

Here is the code:

import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter your gender(Male or Female): ");
        String gender = scanner.next();

        //Extra Spacing
        System.out.println("");

        if (gender == "Male") {
            System.out.println("You're a boy");
        } else if (gender == "Female") {
            System.out.println("You're a girl");
        } else {
            System.out.println("Error, here is some debug info:");
            System.out.println("The value of the condition for the male is: " + (gender == "Male"));
            System.out.println("The value of the condition for the female is: " + (gender == "Female"));
            System.out.println("The gender inputed by the user is: " + gender);
            System.out.println("The input expected was Male or Female");
            System.out.println("The type of the variable gender: " + gender.getClass().getName());
            System.out.println("The type of the string that I am comparing it too is: " + "Male".getClass().getName());
        }
    }
}

For some reason every time that I run the program it doesn't evaluate to Male or Female. For example, if I run the code and write Female, it outputs the following:

Please enter your gender(Male or Female): Female

Error, here is some debug info: The value of the condition for the male is: false The value of the condition for the female is: false The gender inputed by the user is: Female The input expected was Male or Female The type of the variable gender: java.lang.String The type of the string that I am comparing it too is: java.lang.String

What is going on???

Aucun commentaire:

Enregistrer un commentaire