4

I am new to Java so I don't really know much can you help me as simply as possible. This is my code; 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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

4 Answers4

4

Enum is basically declaration of final set of passible options (in your case months). But it is still Java Class ~ Object.

Your error literally says you are missing constructor for Java Class while you want to give every enumeration certain property. I guess you want to add month order in calendar. All you need to do is just declare property of class and enum constructor.

public enum Month {
    // Enum definition. Passing value to constructor. 
    JANUARY(1), FEBRUARY(2), MARCH(3), APRIL(4),MAY(5),JUNE(6), JULY(7), AUGUST(8), SEPTEMBER(9), OCTOBER(10),  NOVEMBER(11), DECEMBER(12);

    // Member field.
    private int monthOrder;

    // Constructor.
    public Month (int monthOrder) {
        this.monthOrder = monthOrder;
    }

    // Accessor.
    public int getMonthOrder() {
        return this.monthOrder;
    }
 }

See similar code run at Ideone.com.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Andrej Buday
  • 559
  • 5
  • 14
2

Add constructor:

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 final int number;

    public Month (int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }
}
Nick
  • 3,691
  • 18
  • 36
2

You have to write constructor for enum. So you need to implement like 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 i) {
        this.value = i;
    }

}

As you can see the constructor ;

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

which is giving the integer value of related month.And set to value field of enum which is keep the month's value.

drowny
  • 2,067
  • 11
  • 19
1

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

}
deHaar
  • 17,687
  • 10
  • 38
  • 51
Pooja Aggarwal
  • 1,163
  • 6
  • 14
  • You should put some explanation in your answer. – CrazySabbath Nov 13 '18 at 11:05
  • As suggested, provided the explanation. Please up vote now if explanation is satisfactory. – Pooja Aggarwal Nov 13 '18 at 11:10
  • The value range of 0 has nothing to do with a value passed into the constructor. It could also have been January(0)... – vikingsteve Nov 13 '18 at 11:25
  • To this point. If you are going to use the enum to index, then I prefer to add the index as a field in the enum (as is shown in this answer). This protects you against simple refactoring like changing the ordering of the enum or inserting new values. – flakes Nov 13 '18 at 11:51