mardi 17 avril 2018

Nested if statement printing incorrectly

The program asks questions and responds accordingly using ArrayLists and if statements. I cannot seem to get an else if statement to print no matter how I tweak it. The code is as follows:

public static void whereAreYouFrom(ArrayList<String> states) {
    System.out.print("Where are you from? ");
    states.add(scanner.next());

    if (states.contains("Florida") || states.contains("florida")) {
        System.out.println("So was I!\n");

    } else if (states.contains("North Carolina") || states.contains("north carolina")) {
        System.out.println("I hear that's a nice place to live.\n");

    } else {
        System.out.println("I would have never guessed!");
    }
}

Inputting Florida or florida prompts the correct response. However, North Carolina or north carolina always prompt "I would have never guessed!" The program is running from a main class:

    ArrayList<String> states = new ArrayList<>();
    whereAreYouFrom(states);

Aucun commentaire:

Enregistrer un commentaire