Help me Subject java (Input month name output number of days)
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.
example
15721f81459504047b4acd5a722a7326

Dani AI

Generated

Short version: accept the month name (case/abbrev tolerant), optionally accept a year for February, and use Java's java.time API to get the correct day count. is right to ask that you show work; 's suggestion to keep data in memory is the right direction here — a small in-memory map is simpler and faster than a database for this problem.

import java.time.Month;
import java.time.YearMonth;
import java.time.format.TextStyle;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class MonthDays {
    private static final Map<String, Month> MONTHS = new HashMap<>();
    static {
        for (Month m : Month.values()) {
            String name = m.name(); // "JANUARY"
            MONTHS.put(name, m);
            MONTHS.put(m.getDisplayName(TextStyle.FULL, Locale.ENGLISH).toUpperCase(Locale.ENGLISH), m);
            MONTHS.put(m.getDisplayName(TextStyle.SHORT, Locale.ENGLISH).toUpperCase(Locale.ENGLISH), m);
            MONTHS.put(name.substring(0,3), m); // "JAN"
            if (name.length() >= 4) MONTHS.put(name.substring(0,4), m); // handles "SEPT"
        }
    }

    public static int daysInMonth(String monthInput, Integer year) {
        if (monthInput == null) throw new IllegalArgumentException("month required");
        String key = monthInput.trim().replaceAll("\\.", "").toUpperCase(Locale.ENGLISH);
        Month month = MONTHS.get(key);
        if (month == null) throw new IllegalArgumentException("unknown month: " + monthInput);
        int y = (year == null) ? 2001 : year; // 2001 is a non-leap-year default
        return YearMonth.of(y, month).lengthOfMonth();
    }
}

Notes and troubleshooting:

  • February needs a year to decide 28 vs 29; if you don't ask for year, return 28 (or explicitly state it's non-leap-year behavior).
  • Trim input, strip dots (e.g., "Feb."), and do case-insensitive matching. The snippet accepts full names, 3-letter and common 4-letter abbrevs like "Sept".
  • For very small programs a simple switch or array also works, but java.time + a small map is safer, clearer, and handles leap years automatically.

Recommended Answers

All 2 Replies

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

make a database, containing the information you need. Then use the user input to query that database and get the data you need to return.

Or just keep a list of things in memory, for something trivial like this that's much easier.

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.