Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Enum handling
    primarykey
    data
    text
    <p>I have a Java question about the best method to work with the following enum file that has been provided to me.</p> <pre><code>public enum Foo{ PLANE(1, "Plane", "plane"), CASTLE(2, "Castle", "castle"), FEILD(3, "Feild", "field"), ROAD(4, "Road", new String[] {"road", "pavement"}); } </code></pre> <p><strong>There are roughly 100ish entries of the above formats</strong></p> <pre><code>/** * Stores a map of the names for fast access. Stores a map of the IDs for fast access. */ private static final Map&lt;Integer,BlockType&gt; ids = new HashMap&lt;Integer,BlockType&gt;(); private static final Map&lt;String,BlockType&gt; lookup = new HashMap&lt;String,BlockType&gt;(); private final int id; private final String name; private final String[] lookupKeys; static { for(Foo type : EnumSet.allOf(Foo.class)) { ids.put(type.id, type); for (String key : type.lookupKeys) { lookup.put(key, type); } } } /** * Construct the type. * * @param id * @param name */ Foo(int id, String name, String lookupKey) { this.id = id; this.name = name; this.lookupKeys = new String[]{lookupKey}; } /** * Construct the type. * * @param id * @param name */ Foo(int id, String name, String[] lookupKeys) { this.id = id; this.name = name; this.lookupKeys = lookupKeys; } /** * Return type from ID. May return null. * * @param id * @return */ public static Foo fromID(int id) { return ids.get(id); } /** * Return type from name. May return null. * * @param name * @return */ public static Foo lookup(String name) { return lookup.get(name.toLowerCase()); } /** * Get block numeric ID. * * @return */ public int getID() { return id; } /** * Get user-friendly name. * * @return */ public String getName() { return name; } </code></pre> <p>So what I am trying to do with this enum is the following: A user issues a command, for example: /bounce 1</p> <p>The program recognizes bounce as a command and looks for the following argument. Since 1 follows the command it now checks that 1 is a valid id# in the enum. If it is it performs the command.</p> <p>A user issues a command for example 2 in the following form: /bounce plane. This time it checks to see if the string is a valid string name in the enum and if it is grab the id# associated with it to perform the command.</p> <p>How can I check for these criteria that either the id or one of the string id's exist and always return the id# for later usage. Any help would be appreciated.</p> <p>It should also be noted that this enum file is it's own independent class file being called from the main.</p> <p>Thanks to all who helped me get on the right track I have awarded Wolfcastle the correct answer, here was what I got to work for me but I am still open to any critiques on logic or performance flaws with my method.</p> <pre><code>String s = null; try { s = in.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { id = Integer.parseInt(s); } catch (NumberFormatException nfe) { } if (BlockType.lookup(s) != null || BlockType.fromID(id) != null) { if (BlockType.lookup(s)!= null) { blockname = BlockType.lookup(s).getName(); blockid = BlockType.lookup(s).getID(); } else { blockname = BlockType.fromID(id).getName(); blockid = BlockType.fromID(id).getID(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload