Results 1 to 11 of 11
  1. #1
    ohbugger is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default Enumerated constants

    I have a class that is an enum that i'm using as a bunch of tokens for some parsing. When my program is finished with the parsing, it outputs to an excel style spreadsheet. Currently, the column heads of this spreadsheet are hard-coded, as strings, and require the user to add them by hand in the correct order. Since there are only 10 or so column heads, this is not a problem. But it will become one, and out of order, missing, or incorrect column heads seem like they will be part of my not-too-distant future if I allow this to continue. So. What i'd like to do is build a piece of code that takes the enumerated constants in the class in the order they are declared, turns each one into a string, and puts it into a list I can use later (I prefer ArrayLists). How would I go about doing this? I'm only somewhat familiar with reflection, so I may need some hand-holding. I can't post the code in question, only similar examples. Thanks in advance for any advice, tutorials, suggestions, etc. If its tutorial responses, be aware that i've already googled my way through the first two pages of "turning enumerated constants into a list of strings."

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    Why do you want them as a list of Strings?
    Why not simply use the enum itself?
    You can get the ordered set using EnumSet.
    As for turning each one into a string, then simply use toString(). You can override that as well, if you don't simply want to use the name of the enum.

    And getting them? Use valueOf().

  3. #3
    ohbugger is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    As strings, so that they can be used as the column heads in the excel spreadsheet. I'm not going to use the enum itself, as that has the same pitfalls as writing out the columnheads long form like I have it now.

    So ClassName.THISTOKEN.toString() would be worthless -- it incurs the same problems.

    What i'm looking for is to create an ArrayList or something similar of every single enum in the Class, in order. So that I needn't make changes to the code each time I add a new Token.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    I still don't understand how that helps.
    How do these tokens (your enum) relate to the column headings?

    How does having a separate list of Strings help?

    Indeed, how is your idea of converting the enums into a list of Strings any different than simply using the EnumSet and toString on the enums?

    This is th bit that I don't understand:
    What i'd like to do is build a piece of code that takes the enumerated constants in the class in the order they are declared, turns each one into a string, and puts it into a list I can use later (I prefer ArrayLists).
    That list will be the same as an EnumSet of your enum.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    Hang on.
    This is an enum you're talking about isn't it and not a class with a load of static constants in it?

  6. #6
    golan2 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Post Have you tried parameterized enum?

    Hi,

    I am not sure I understand your question but see my example below.
    I have a "Tokens" enum with names {A.B.C} but the values they hold are "nice" values so when you do Tokens.A.toString() you don't get "A" rather than "Nice name 1".

    ================================================== =
    == BEGIN

    public class MainThread extends TestCase {

    public static void testEnum() {
    final List<Tokens> tokens = Arrays.asList(Tokens.values());
    for (Tokens token : tokens) {
    System.out.println(token);
    }
    }

    private static enum Tokens {
    A ("Nice name 1"),
    B ("Nice name 2"),
    C ("Nice name 3");

    private final String niceName;

    private Tokens(String niceName) {
    this.niceName = niceName;
    }

    @Override
    public String toString() {
    return this.niceName;
    }
    }
    }

    == END
    ================================================== =

    Running this code above will result in the following output:

    Nice name 1
    Nice name 2
    Nice name 3

  7. #7
    ohbugger is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I'm going to try Golan's code, it looks kind of like what I was trying to put together myself.

    However, I'm all for learning new things, so let me see if I understand this:

    I'm not familiar with enumsets, and in looking through the java specs, I'm not sure I understand how one could make use of that either --

    Would an enumset of my Tokens class, for example
    enumSet<Tokens> create a set of all of the tokens, in a Set format?

    So I would just be able to for:each through the created set, outputting each with toString() ?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    So I would just be able to for:each through the created set, outputting each with toString() ?

    You don't even need an enum set for that. Just use the values() method:

    Java Code:
    for(TheEnum e :TheEnum.values()) {
        System.out.println(e);
    }

    Of course you can do other things instead of printing them ... like making them the column headers of your table.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    So, is this answered then?
    You can just use the enum without having to create some additional List.

  10. #10
    ohbugger is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    You don't even need an enum set for that. Just use the values() method:

    Java Code:
    for(TheEnum e :TheEnum.values()) {
        System.out.println(e);
    }

    Of course you can do other things instead of printing them ... like making them the column headers of your table.
    Wow, I totally didn't know you could iterate through them like this -- where did you pick up this bit of information? i'm interested in more like it.
    Second question, while i'm on the topic of iteration and reflection -- Is there a simple way to call only the get members of a class, one after the other in the order they're declared?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    where did you pick up this bit of information? i'm interested in more like it.

    The 1.5 guide notes are a bit more detailed than the section in Oracle's Tutorial. (Although it is worth reading, and does mention the values() method.)

    There are API docs for EnumSet and EnumMap of course.

    Finally, dry, technical and not guaranteed to be easily understandable, but - by definition - correct and complete is the Java Language Specification: 8.9 Enums.

    I mention all these because they are pretty much where I go if I can't understand something. (Not always though: there are better glosses for, say, generics.) Whether the JLS is an appropriately new-to-java resource is a matter of debate. But knowing that it exists, and knowing your way around it is worthwhile: just don't get hung up on it. The API docs, although daunting, are to be consulted: from day one and on each day thereafter.

    [Edit] Sorry - just noticed that this isn't the NTJ forum! Doesn't change things much: some people would look anywhere else for information and leave the JLS to language lawyers. But it is an important resource and only sometimes inpenetrably dense.
    Last edited by pbrockway2; 01-29-2011 at 01:54 AM.

Similar Threads

  1. Enumerated Types
    By muhsy in forum Advanced Java
    Replies: 2
    Last Post: 01-27-2011, 08:18 AM
  2. Problem using enumerated types
    By Stryker4526 in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 03:57 AM
  3. Replies: 0
    Last Post: 08-17-2009, 03:59 PM
  4. How to define constants in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:39 PM
  5. Global constants
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 09:06 AM

Posting Permissions

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