vendredi 3 mars 2017

So I want to get input from a user using the console and check if it is equal to "hi" but for some reason the if statement isn't working [duplicate]

This question already has an answer here:

import java.util.Scanner;

public class testIf {

public static void main(String[] args) {
    System.out.println("Hello");

    // Declaring scanner 
    Scanner scanner = new Scanner(System.in);

    // I type in hi in to the console
    String greeting = scanner.nextLine();

    //Checking input
    if (greeting == "hi") 
    {
        System.out.println("good");
    } 
    else 
    {
        System.out.println("bad");
    }   
}

}

// output: bad`enter code here

Aucun commentaire:

Enregistrer un commentaire