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");
        }
    }

If the user is to enter the name of the month, you will have to read a String as input from the user.

create a string array and search the list for the position in the array.

Your idea about the String array sounds ok. Where is the problem now?

I don't quite know where to start.
I didn't inclue my original attempt at this problem for the sake of others being able to work off of the original program.
My attempt looked messy in the first place

but it looked something like this

    String[] monthName = new String [] {"", "January", "February", "March", "April", "May", "June", "July", 
         "August", "September", "October", "November", "December"};
    int month = 0;
    SimpleIO.prompt ("Enter a month name: ");
    String userInput = SimpleIO.readLine (). trim ();
    if (monthName[].equals((userInput)) {
        month = monthName[userInput];
        else {
            if (monthName[].equals(!userInput)) {
                System.out.println ("Illegal month name; try again.");
                return;
            }
        }
    }

To look at all the elements in an array you will need to use a loop like a for loop. Use the loop control variable as index to the array to get at each element in the array and compare it to the user's input.

To start, get the user's input of the mpnth name and use a loop to find the location of that input in the array. Then print out the index if it is found in the array or an error message if it is not found

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.