mercredi 22 janvier 2020

Why does if statement always goes to else whenever I'm running this type of code: [duplicate]

import java.util.Scanner;

public class sampleCode {
    public static void main(String[] args) {

        String a = "a. Hello";
        String b = "b. Goodbye";

        System.out.println("Make your Choice:");
        System.out.println("a. Hello");
        System.out.println("b. Goodbye");

        Scanner input = new Scanner(System.in);
        String entry = input.nextLine();

        if (entry == a || entry == "a") {
            System.out.println("How are you");
        } else {
            System.out.println("See you");
        }
        input.close();
    }
}

as you can see whenever you choose (let's say "a") the program always outputs the else statement. Why?

Aucun commentaire:

Enregistrer un commentaire