Sun's demo using enum doesn't seem to work for me
I'm trying to run a demo from the Sun website, http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html, which includes the enum statement:
publicclass SwitchEnumDemo{
publicenum Month{ JANUARY, FEBRUARY, MARCH, APRIL,
MAY, JUNE, JULY, AUGUST, SEPTEMBER,
OCTOBER, NOVEMBER, DECEMBER}
publicstaticvoid main(String[] args){
Month month = Month.FEBRUARY;
int year = 2000;
int numDays = 0;
switch (month){
case JANUARY:
// etc etc ...
}
System.out.println("Number of Days = " + numDays);
}
}
However, copying-and-pasting the code into NetBeans, I get an error on the enum declaration stating: '';' expected. Warning: as of release 1.5, 'enum' is a keyword and may not be used as an identifier'. Well... Iknow that. Isn't that why I'm using it in the first place? Or am I confused?
I'm using NeBeans 5.0 on Java 1.5.0_06.
Any advice?
Question 2: Once I get this thing working, is there any way I can get the month as an input from the user, without needing a long block stating
if (input ="January") month = Month.JANUARY;
elseif (input ="Feburary") month = Month.FEBURARY;
//etc etc
That is, can the string representation of a month be somehow kept within the enumerated Month itself, and that be used to check the user's input?
Thanks for any advice!
[2257 byte] By [
Asbestosa] at [2007-10-2 20:13:45]

> However, copying-and-pasting the code into NetBeans,
> I get an error on the enum declaration stating:
> '';' expected. Warning: as of release 1.5, 'enum'
> is a keyword and may not be used as an
> identifier'. Well... I know that. Isn't
> that why I'm using it in the first place? Or am I
> confused?
>
> I'm using NeBeans 5.0 on Java 1.5.0_06.
I can't say for sure about that; it seems very odd. However, I do know that my IDE will warn me about those sorts of things if I configure my project for pre-1.5 operation. It allows me to say a project is for Java 1.4 even if I'm using a 1.5 JVM and will mention things like that so that forward compatibility can be considered. I don't suppose this could be the case with your situation?
You might want to search for all instances of "enum" in your code, though, because it's hard to imagine the one instance which appears in the snippet you posted causing problems.
> Question 2: Once I get this thing working, is there
> any way I can get the month as an input from the
> user, without needing a long block stating
{snip}
Well, in this case you can just do
for (Month m : Month.values())
{
if (m.toString().equalsIgnoreCase(input))
{
month = m;
break;
}
}
or you can build a Map<String,Month> if you're looking for case-sensitive comparison.
In general, however, you can put pretty much anything into an enum:
public enum SomeEnum
{
A(0),
B(0),
C(1),
D(0),
E(2);
private int value;
SomeEnum(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
}
// the following is perfectly legal
SomeEnum e = methodThatReturnsSomeEnum();
System.out.println(e.getValue());
and you could use that to put some form of identifier in the enumeration object. I have a class around here somewhere which uses an enum to enumerate the operators in an expression parser; the Operator enum constructor accepts an int specifying the operator precedence.
tvynra at 2007-7-13 22:55:53 >

You probably need to configure NetBeans to tell it you want to use Java 5 features. Can't help much there as I don't use it myself.
As to your other question, the toString() representation of an enum is the same as its short name. For example:
public class Java5Enums {
public enum Foo {
BAR,
SPONG
}
public static void main(String... args) {
System.out.println(Foo.BAR);
}
}
However I'd be a bit cagey about using this approach myself. Amongst other things it doesn't lend itself to language translation.
(edit) So old, and yet so slow... :-(
> I can't say for sure about that; it seems very odd.
> However, I do know that my IDE will warn me about
> those sorts of things if I configure my project for
> pre-1.5 operation. It allows me to say a project is
> for Java 1.4 even if I'm using a 1.5 JVM and will
> mention things like that so that forward
> compatibility can be considered. I don't suppose
> this could be the case with your situation?
Actually, I guess it could be. I was dumb and didn't quote the error message in it's entirety:
';' expected
Warning: As of release 1.5, 'enum' is a keyword and may not be used as an identifier.
(try -source 1.5 to use 'enum' as a keyword)
Does this mean that NetBeans is not actually running my project in 1.5? When I click "about" it tells me my java is 1.5.0_06, but could it actually be using 1.4 or whatever? How would I correct this (I'm not sure where I'd put '-source 1.5' when running from NetBeans).
By the way, the code in the original post is the code in it's entirety - there are no other uses of 'enum' in the program.
So how would I make sure NetBeans is running my project in 1.5?
I'm not sure exactly how to do that in NetBeans (I use IntelliJ myself). However, it definitely sounds like that's your problem. And the "About" popup telling you that your VM is 1.5.0_06 is probably just running -version on the command line. The 1.5.0_06 compiler is perfectly capable of pretending to be a 1.4 compiler.
I would guess that the problem would lie in your project/module settings somewhere; adding -source 1.5 to the command line would probably not be sufficient as NetBeans is likely adding -source 1.4 itself.
tvynra at 2007-7-13 22:55:53 >

Ok. I think probably I'm better of asking on the NetBeans listserve. Thanks for your help!
No problem. Best of luck.Isn't there a forum around here for NetBeans or something?
tvynra at 2007-7-13 22:55:53 >

I'm sure the original poster long since resolved this problem ...
But just I ran into the same thing trying to use NetBeans 5.0/JDK 1.5.06 to develop a servlet app.
J2EE was configured to use Java 5.0 Project Explorer showed the correct "build. compile" options.
If I compiled "MyServlet.java" separately, it worked (no enum error). But if I did a clean/build of the entire project, it failed:
JdbcDb.java:28: warning: as of release 1.5, 'enum' is a keyword
SOLUTION:
The problem was the following two lines in the auto-generated "project.properties "file =>
javac.source=1.5
javac.target=1.5
<= CHANGED THESE FROM "1.4" TO "1.5"!!!!
An other way to up the source-level that NetBeans uses is, in the "Projects" window, right click on the coffee up (or project name), and select "Properties". Then, select the "Sources" tab, and all at the bottom of the page there's a "Source Level: ..." box, where one can change the "1.4" to "1.6".