Results 1 to 5 of 5
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default How do I easilt check if a string is inside a enum?

    Can I do like this:
    PHP Code:
    ...
    if ("Hi" in words){...
    ?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    You are not making much sense. An enum is not a collection.

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    no, so i g2 do a for loop to check all values in the enum?

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    8

    Default

    You could if you really really want to, but there probably better ways other then enum like with lists and other data stuctures:

    Java Code:
    public enum type {One, Two, Three}
    
    public static boolean inEnumType(String name)
    {
       try
       {
          type.valueOf(name);
          return true;
       }
       catch(Exception e)
       {
          return false;
       }
    }
    
    ...
    public static void main(String[] args)
    {
       System.out.println("One exists: " + inEnumType("One"));
       System.out.println("Two exists: " + inEnumType("Two"));
       System.out.println("Three exists: " + inEnumType("Three"));
       System.out.println("Four exists: " + inEnumType("Four"));
    }
    My Hobby Project: LegacyClone

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

Similar Threads

  1. the check file inside of the zip in javascript
    By naval gupta in forum Advanced Java
    Replies: 1
    Last Post: 07-18-2009, 12:12 PM
  2. check if String is an integer?
    By McChill in forum New To Java
    Replies: 5
    Last Post: 05-02-2009, 07:51 PM
  3. [SOLVED] Getting the value inside a SQL Query into a String.
    By mainy in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 03-01-2009, 11:08 AM
  4. Enum example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:34 PM
  5. string inside of a xml
    By Heather in forum XML
    Replies: 2
    Last Post: 03-28-2008, 06:21 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
  •