Question

so i'm asking the user for a month and a year. month has to be one of the twelve months and year has to be a number and no letters. i'm trying to figure out the best way to make the program say "wrong input, try again" and prompt them for input again. here's the section of code i'm working with for the month section.

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

     int month = 0;
    // Prompt the user to enter a month
    SimpleIO.prompt("Enter a month name: ");
    String userInput = SimpleIO.readLine();

      if (userInput.trim().toLowerCase().equals("january")) {
        month = 1;
      } else if (userInput.trim().toLowerCase().equals("february")) {
        month = 2;
      } else if (userInput.trim().toLowerCase().equals("march")) {
        month = 3;
      } else if (userInput.trim().toLowerCase().equals("april")) {
        month = 4;
      } else if (userInput.trim().toLowerCase().equals("may")) {
        month = 5;
      } else if (userInput.trim().toLowerCase().equals("june")) {
        month = 6;
      } else if (userInput.trim().toLowerCase().equals("july")) {
        month = 7;
      } else if (userInput.trim().toLowerCase().equals("august")) {
        month = 8;
      } else if (userInput.trim().toLowerCase().equals("september")) {
        month = 9;
      } else if (userInput.trim().toLowerCase().equals("october")) {
        month = 10;
      } else if (userInput.trim().toLowerCase().equals("november")) {
        month = 11;
      } else if (userInput.trim().toLowerCase().equals("december")) {
        month = 12;
      }


    // Terminate program if month is not a proper month name
    if (month < 1 || month > 12) {
      System.out.println("Illegal month name; try again");
      return;
    }

and here's what i'm working with for the year section:

    // Prompt the user to enter a year
    SimpleIO.prompt("Enter a year: ");
    userInput = SimpleIO.readLine();
    int year = Integer.parseInt(userInput);

    //Here, trying to use hasNextInt to make sure input is an integer
    //If it's not, need to give an error message and prompt input again
    // public boolean hasNextInt()

    //Prompt input again if year is negative
    if (year < 0) {
       System.out.println("Year cannot be negative; try again");
       return;
    }

    // Determine the number of days in the month
    int numberOfDays;
    switch (month) {
      case 2:  // February
               numberOfDays = 28;
               if (year % 4 == 0) {
                 numberOfDays = 29;
                 if (year % 100 == 0 && year % 400 != 0)
                   numberOfDays = 28;
               }
               break;

      case 4:  // April
      case 6:  // June
      case 9:  // September
      case 11: // November
               numberOfDays = 30;
               break;

      default: numberOfDays = 31;
               break;
    }

    // Display the number of days in the month
    System.out.println("There are " + numberOfDays +
                       " days in this month");
  }
}

after seeing the code i'm sure it will be more clear what i'm asking. if they enter a word that isn't a month, prompt them and ask for input again. same thing if they enter a year that isn't integers. thanks in advance!

No correct solution

OTHER TIPS

Running it in a loop, will do:

String userInput;
int month;
do{
    SimpleIO.prompt("Enter a month name: ");
    userInput = SimpleIO.readLine();
    try{
        month = Integer.parseInt(userInput);
    } catch(NumberFormatException e){
        continue;
    }
}while(month <= 0 || month > 12);

You should create a loop that keeps prompting the user until the month is correctly inserted. Something in the following lines:

boolean correct_month = false; // Control variable 

while(!correct_month)
{
   int month = 0;
    // Prompt the user to enter a month
    SimpleIO.prompt("Enter a month name: ");
    String userInput = SimpleIO.readLine();

     ...
   // If the month is indeed correct
   // then correct_month = true;
}

Then you apply the same idea to the years.

Instead of having all of those conditions on the month, I think it is better to add all the month strings into an ArrayList:

ArrayList <String> all_months = new ArrayList <String> ();

and then you just have to use all_months.indexOf with the string insert by the user. If it returns -1 the string is not a valid month, otherwise, it will give you the position where the month is on the list. For example

month = all_months.indexOf(userInput);
if(month != -1){
   correct_month = true;
}

Thus, the complete solution would be something like:

ArrayList <String> all_months = new ArrayList <String> ();
all_months.add("january");
... // and so one

int month = 0; // Control variable 

while(month <= 0)
{

    // Prompt the user to enter a month
    SimpleIO.prompt("Enter a month name: ");
    String userInput = SimpleIO.readLine();

    month = all_months.indexOf(userInput.trim().toLowerCase()) + 1;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top