Results 1 to 18 of 18
  1. #1
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Thumbs up ...? 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());

    }
    }

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    What kind of errors are you getting? I was able to run the code without a problem.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    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.

  4. #4
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    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?

  5. #5
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    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

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

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

  7. #7
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    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

  8. #8
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    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!

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    What version of Java are you using?

  10. #10
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    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?

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I believe enums were introduced in Java 1.5. Time to update your version.

  12. #12
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    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?

  13. #13
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    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 :)

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Why on earth would you want to get JRE 1.1?

    Go to the java downloads page and download the latest JDK version.

  15. #15
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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!

  16. #16
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    no guys i am using Eclipse on Linux.
    It has all versions available in it.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  18. #18
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Replies: 7
    Last Post: 03-05-2011, 09:25 AM
  3. Is the Head First Java book wrong about Strings and GC?
    By raindog308 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 10:05 PM
  4. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  5. Replies: 0
    Last Post: 05-26-2008, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •