dimanche 11 février 2018

String comparison in inside if else fails

import java.util.Scanner;

public class BeispielBundesländer {

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    System.out.println("How many states are there?");
    int states = scan.nextInt();
    String president = "Test Person";

    if (states == 9) {
        System.out.println("Good Job!");
        System.out.println("Now tell me the presidents name!");

        String presidentInput = scan.nextLine();

        if (presidentInput.equals(president)) {
        System.out.println("Well done!");
        } else {
            System.out.println("Fail!"); }
    } else {
        System.out.println("Fail!");
            }
}
}

The string comparison in the second if 'presidentInput.equals(president)' never works. I tried it with "String presidentInput = scan.next();" but the problem with that is that it only saves the first word e.g. "Test" in the variable presidentInput instead of "Test Person". So i tried nextLine() but with that doesn't even let me write anything, just jumps the fail.

Aucun commentaire:

Enregistrer un commentaire