Results 1 to 18 of 18
Thread: ...? my text book is wrong?
- 04-07-2011, 02:50 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
...? my text book is wrong?
Hi guys, today I was learning about enum from my text book and tried copying the example that was given in the book. However the problem was that there were errors instead of expected output. Could you guys look over the code and see if the book was wrong or I am just doing something incorrectly?
Oh, BTW i've been looking at some enum examples and other people wrote:
"public enum something {something1, something2, etc}"
but my book example did not have the "public" in the front.
If the book was wrong, would you guys mind helping me out fixing this so that I could learn from it?
BOOK EXAMPLE:
:confused:
/* Demonstrates the use of enumerated types. */
public class listing0306 {
enum Flavor {
vanilla, chocolate, strawberry, fudgeRipple, coffee, rockyRoad, mintChocolateChip, cookieDough
}
// Creates and uses variables of the Flavor type.
public static void main (String[] args){
Flavor cone1, cone2, cone3;
cone1 = Flavor.rockyRoad;
cone2 = Flavor.chocolate;
System.out.println ("cone1 value: " + cone1);
System.out.println ("cone1 ordinal: " + cone1.ordinal ());
System.out.println ("cone1 name: " + cone1.name());
System.out.println ();
System.out.println ("cone2 value: " + cone2);
System.out.println ("cone2 ordinal: " + cone2.ordinal ());
System.out.println ("cone2 name: " + cone2.name());
cone3 = cone1;
System.out.println ();
System.out.println ("cone3 value: " + cone3);
System.out.println ("cone3 ordinal: " + cone3.ordinal ());
System.out.println ("cone3 name: " + cone3.name());
}
}
- 04-07-2011, 03:25 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
What kind of errors are you getting? I was able to run the code without a problem.
- 04-07-2011, 04:26 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
Yes.
Don't just tell us it doesn't work.
Tell us what doesn't work...what you are seeing and what you expect to see.
- 04-08-2011, 12:02 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
Oh sorry guys, I was trying to work on it and I guess I forgot to tell you guys what the actual problem was.
What the problem is that I am not getting the result and I get this message:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Flavor cannot be resolved to a type
Cannot make a static reference to the non-static field Flavor
Cannot make a static reference to the non-static field Flavor
at listing0306.main(listing0306.java:12)
after typing in enum, shouldn't it be colored in different color (I am currently using Eclipse).
instead I am getting a red underline beneath enum and Eclipse is telling me that there is an error to that command.
Now does that help explain the situation a little better?
- 04-08-2011, 12:03 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
My expected result was the Output:
cone1 value: rockyRoad
cone1 ordinal: 5
cone1 name: rockyRoad
cone2 value: chocolate
cone2 ordinal: 1
cone2 chocolate
cone3 value: rockyRoad
cone3 ordinal: 5
cone3 name: rockyRoad
- 04-08-2011, 12:12 AM #6
looks like you have the classic static/non-static problem. Try creating an instance of a class.
Java Code:class Foo { // enum goes here public void run() { // code currently in main goes here } public static void main(String[] args) { new Foo().run(); } }
- 04-08-2011, 12:37 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
hmmm that didn't seem to help.
I tried doing what you told me to do and it still wouldn't recognize enum command.
I think that the main problem is that my Eclips won't recognize enum command for some reason.
I don't really know how to create an instance of class yet either... dam i hate being insufficient
- 04-08-2011, 12:46 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
okay quick question guys.
When I was working on these examples and personal project assignments from the text book, I was just creating new "class" within a src folder...
I am that was the problem?
If it is, then how am I suppose to make a new project in Eclipse?
Sounds like a stupid question, but after reading through the help section with Eclipse, it only created more confusion in my part.
So if someone could answer me that, I guess that will be more helpful.
thanks guys!
- 04-08-2011, 12:47 AM #9
What version of Java are you using?
- 04-08-2011, 12:49 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
i was using J2SE-1.4 version.
When I was going through the Eclipse tutorial it suggested that I use that version.
Am i using a wrong version?
- 04-08-2011, 12:53 AM #11
I believe enums were introduced in Java 1.5. Time to update your version.
- 04-08-2011, 12:57 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
ah hah!
then which one should i update it to then? the latest one? OSGi/Minimum-1.2?
Or should I update it to J2SE 1.5?
Or should I update it to JRE 1.1?
- 04-08-2011, 12:59 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
oh and if someone could tell me the differences between OSGi/Minimum, J2SE, and JRE (the short version) then it will be really helpful too :)
- 04-08-2011, 01:10 AM #14
Why on earth would you want to get JRE 1.1?
Go to the java downloads page and download the latest JDK version.
-
Latest is 1.6.24 (Java SDK 6, build 24) but Java 7 is coming out pretty soon so your version 4 is very old!
- 04-08-2011, 01:30 AM #16
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
no guys i am using Eclipse on Linux.
It has all versions available in it.
- 04-08-2011, 01:51 AM #17
It does?
If you have multiple Java versions installed then you need to change the properties of your project to compile with 1.5 or later.
- 04-08-2011, 02:01 AM #18
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
Transforming xml to text keeps placing blank line at beginning of text file
By DerekRaimann in forum Advanced JavaReplies: 7Last Post: 03-05-2011, 09:25 AM -
Is the Head First Java book wrong about Strings and GC?
By raindog308 in forum New To JavaReplies: 2Last Post: 02-07-2011, 10:05 PM -
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By luislopezco in forum Advanced JavaReplies: 0Last Post: 05-26-2008, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks