I'm completely lost with this assignment code.
The objective is to modify this program that originally asked the user for an input 1-12 for a month, and it outputs the number of days in that month.
The modifications would make it so that the user inputs the name of the month instead of the value, and the program outputs the number of days.
I also have to incorporate lines that asks the user to reenter the month if it is not a valid month, and to reenter the year if if the year is < 0 (negative).
I think the obvious method to tackle this code is to create a string array and search the list for the position in the array. Then use that position for the switch statement. But after hours of trying to crack this I've had no luck.
Thank you.
import jpb.*;
public class MonthLength1 {
public static void main (String[] args) {
SimpleIO.prompt ("Enter a month (1-12) : ");
String userInput = SimpleIO.readLine ();
int month = Integer.parseInt (userInput);
if (month < 1 || month > 12) {
System.out.,println ("Month must be between 1 and 12");
return;
SimpleIO.prompt ("Enter a year: ");
userInput = SimpleIO.readLine ();
int year = Integer.parseInt (userInput);
if (year < 0) {
System.out.println ("Year cannot be negative; try again.");
return;
}
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:
case 6:
case 9:
case 11:
numberOfDays = 30;
break;
default: numberOfDays = 31;
break;
}
System.out.println ("There are " + numberOfDays + " days in this month");
}
}