-4

I have tried to do a robust code on java but it doesn't seem to work. What I am looking for is for a user to enter an input, the program will check the input if it is not a required input, then the user will be given an option to reenter the appropriate input until the input match to the required inputs, or simply quit. Here is what I have so far. When I run this code, everything works well except when a user enters a wrong input and wants to quit. The while loop keeps running and not stopping even when a user reenter a proper input or quit. How can I make this work?

    //question
    System.out.println("Summer, Winter, Fall, or Spring");
    System.out.print("Which season is your favarite? ");
    String favSeason = in.next();
    System.out.println();

    //Control the inputs by converting them to Upper Case
    String favSeasonInput = favSeason.toUpperCase();

    //required answers of the question
    String seasons = "SUMMER, WINTER, FALL, SPRING?";
    String quit = "QUIT!";

    boolean isSeasons = (favSeasonInput.equals(seasons.substring(0, 6)) ||
            favSeasonInput.equals(seasons.substring(8, 14)) ||
            favSeasonInput.equals(seasons.substring(16, 20)) ||
            favSeasonInput.equals(seasons.substring(22, 28)));
    boolean isQuit = favSeasonInput.equals(quit.substring(0, 4));
    //inialize variables that will compute scores
    int favSeasonScore = 0;

    //if user enters an input otherthan seasons
    while (!isSeasons){

        favSeason = in.next();

        if(isQuit){
            System.exit(0); 
        }

    }


    //Conditions to set up scores for seasons
    if(favSeasonInput.equals(seasons.substring(0, 6))){
        favSeasonScore = 6;
        System.out.println("Summer is " + favSeasonScore + " points");
    }
    else if(favSeasonInput.equals(seasons.substring(8, 14))){
        favSeasonScore = 14;
        System.out.println("Winter is " + favSeasonScore + " points");
    }
    else if(favSeasonInput.equals(seasons.substring(16, 20))){
        favSeasonScore = 20;
        System.out.println("Fall is " + favSeasonScore + " points");
    }
    else if(favSeasonInput.equals(seasons.substring(22, 28))){
        favSeasonScore = 28;
        System.out.println("Spring is " + favSeasonScore + " points");
    }

    System.out.println(favSeasonScore);

1 Answers1

0

The problem is that you don't update the values of your Boolean variables when you read new input; you don't even read it into the same variable.

So:

favSeason = in.next();

Should be:

favSeasonInput = in.next().toUpperCase();
isSeaons = ...;
isQuit = ...;

But note that this way of checking for valid input is awful. It is highly inefficient (pulling out substrings on each check), but also really brittle (you've got to get those indexes right), and you have to update the code in multiple places when your requirements change.

You are mapping a string to an integer, so use a Map:

Map<String, Integer> seasonScores = new HashMap<>();
seasonScores.put("SPRING", 28);
// Etc.

Then your isSeason variable becomes:

isSeason = seasonScores.keySet().containsKey(favSeasonInput);

And your conditionals go away, becoming:

seasonScore = seasonScores.get(favSeasonInput);
Andy Turner
  • 137,514
  • 11
  • 162
  • 243