Java Month enum

Thomas Arif Başoğlu :

Hi guys I am new to Java so I don't really know much can you help me as simply as possible so this is my code and I got an error about the month(int) so that means it cannot import the library.

public enum Month {
    January(1), February(2), March(3), April(4),May(5),June(6), July(7), August(8), September(9), October(10),  November(11), December(12)
}

ERROR:Description Resource Path Location Type The constructor Month(int) is undefined Month.java /tb00594_comp1027_formative2/src/tb00594_comp1027_formative2 line 4 Java Problem

WARNING:Description Resource Path Location Type Build path specifies execution environment JavaSE-1.7. There are no JREs installed in the workspace that are strictly compatible with this environment. tb00594_comp1027_formative2 Build path JRE System Library Problem

So if you can help me as quickly as possible I would be greatefull.

Pooja Aggarwal :

The Value you are providing in enum is by default 0, 1,... an so on as in array indexing. If you want to change this, you need to have value integer taken explicitly and add it in constructor as well. Also, to use this value you can have a getter as well.

Try this:

public enum Month {

    January(1),
    February(2),
    March(3),
    April(4),
    May(5),
    June(6),
    July(7),
    August(8),
    September(9),
    October(10),
    November(11),
    December(12);

    private int value;

    Month(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=114062&siteId=1