Hi, I'm currently working on a GUI programme that caluculates the amount of days in a month through user input.
However I've become stuck where the programme crashes upon entering the month number.

I believe that I may have incorrectly chosen my instance variables but I am unsure of which ones.
Any help is greatly appreciated.

here's what I have done so far.

   String monthName[] = {"January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December"};

    String monthString;

        int month = 8;

        switch (month) {

            case 1:
                monthString = "January";
                break;
            case 2:
                monthString = "February";
                break;
            case 3:
                monthString = "Mrach";
                break;
            case 4:
                monthString = "April";
                break;
            case 5:
                monthString = "May";
                break;
            case 6:
                monthString = "June";
                break;
            case 7:
                monthString = "July";
                break;
            case 8:
                monthString = "August";
                break;
            case 9:
                monthString = "September";
                break;
            case 10:
                monthString = "October";
                break;
            case 11:
                monthString = "November";
                break;
            case 12:
                monthString = "Decemer";
                break;
            default:
                monthString = "Invlid Month";
                break;
        }

        switch (month) {

            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                int numDays = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30;
                break;
                case 2:
                    numDays = 29;
                break;
            default:
                System.out.println("Invalid month.");
                break;
        }



        String[] result =monthName;
        resultLabel.setText(String.format("Result: %.2f", result));
    }                                               




 private void calculatebuttonActionPerformed(java.awt.event.ActionEvent evt) {                                                

         String monthName[] = {"January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December"};

    String monthString;

        int month = 8;

        switch (month) {

            case 1:
                monthString = "January";
                break;
            case 2:
                monthString = "February";
                break;
            case 3:
                monthString = "Mrach";
                break;
            case 4:
                monthString = "April";
                break;
            case 5:
                monthString = "May";
                break;
            case 6:
                monthString = "June";
                break;
            case 7:
                monthString = "July";
                break;
            case 8:
                monthString = "August";
                break;
            case 9:
                monthString = "September";
                break;
            case 10:
                monthString = "October";
                break;
            case 11:
                monthString = "November";
                break;
            case 12:
                monthString = "Decemer";
                break;
            default:
                monthString = "Invlid Month";
                break;
        }

        switch (month) {

            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                int numDays = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30;
                break;
                case 2:
                    numDays = 29;
                break;
            default:
                System.out.println("Invalid month.");
                break;
        }



        String[] result =monthName;
        resultLabel.setText(String.format("Result: %.2f", result));
    }                                

The above code is for the calculte button, the result of which will be outputted to the
result label

As always: keep your code as simple as possible.

For instance:

     String monthName[] = {"January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December"};
String monthString;
    int month = 8;
    switch (month) {
        case 1:
            monthString = "January";
            break;
        case 2:
            monthString = "February";
            break;
        case 3:
            monthString = "Mrach";
            break;
        case 4:
            monthString = "April";
            break;
        case 5:
            monthString = "May";
            break;
        case 6:
            monthString = "June";
            break;
        case 7:
            monthString = "July";
            break;
        case 8:
            monthString = "August";
            break;
        case 9:
            monthString = "September";
            break;
        case 10:
            monthString = "October";
            break;
        case 11:
            monthString = "November";
            break;
        case 12:
            monthString = "Decemer";
            break;
        default:
            monthString = "Invlid Month";
            break;
    }

is nothing but:

       String monthName[] = {"January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December"};
    int month = 8;
    String monthString = (month < 1 || month > 12 ) "Invalid month" :  monthName[month-1];

it's easier to debug 10 lines of code compared to debugging 200 lines.
is resultLabel instantiated anywhere?

secondly: what's the use of this:

String[] result =monthName;
resultLabel.setText(String.format("Result: %.2f", result));

first of all, duplicating the array is pointless.
so, shorter:

resultLabel.setText(String.format("Result: %.2f", monthName);

I don't see you adding the number of the days anywhere to the output, wasn't that the point?

This article has been dead for over six months. Start a new discussion instead.