Results 1 to 6 of 6
  1. #1
    SVTermiCobra is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default How to set valid types for variable

    Last newbie question then I'm good

    How do you set valid types for a variable?

    The scenario is I have a variable called "category"

    Java Code:
    String category;
    But I want to set it up so that it only accepts 3 things: "Temporary", "Part-time", and "Full-time".

    I have tried an IF statement but it only checks for one variable, not all 3. So how do I code so that when creating an object it checks to make sure the input is one of the 3?

  2. #2
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: How to set valid types for variable

    you mean you want a variable that store 3 possibilities? just like a boolean, but with 3 possibilities? enum types are the answer ;)
    Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. #3
    SVTermiCobra is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: How to set valid types for variable

    Quote Originally Posted by Chri View Post
    you mean you want a variable that store 3 possibilities? just like a boolean, but with 3 possibilities? enum types are the answer ;)
    Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    oh thanks! That clears a little up for me but now I'm trying to see how to utilize it as a variable. Can I do something like:

    public enum category {"Temporary", "Half-Time", "Full-Time"}

    I tried this but I get an error because of the "-" dashes, and I need these included for the input.
    Last edited by SVTermiCobra; 08-08-2012 at 05:07 AM.

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: How to set valid types for variable

    Enum Types are used to represent constants. Did you read the link that Chri provided?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to set valid types for variable

    Yes, read the tutorial. Enums are not the same as Strings. You would use constant identifiers, and the same rules for naming variables and constants apply for enum identifiers. Note that the enum objects can hold Strings in fields -- see the tutorial to see how they can hold fields.

  6. #6
    SVTermiCobra is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: How to set valid types for variable

    Thanks all, yes I read the tutorial but it still seems foreign to me trying to comprehend the usage of it. I will keep working on it though!

Similar Threads

  1. Variable types and serializing
    By christine in forum Advanced Java
    Replies: 4
    Last Post: 05-31-2012, 01:47 AM
  2. Variable types
    By Eleeist in forum New To Java
    Replies: 4
    Last Post: 07-27-2011, 06:04 PM
  3. Confusion with variable types
    By popeyito18 in forum New To Java
    Replies: 4
    Last Post: 07-01-2011, 05:58 PM
  4. Unknown Variable Types
    By Slayer in forum New To Java
    Replies: 6
    Last Post: 10-25-2010, 05:44 AM
  5. Dynamic types for a variable.
    By Somelauw in forum New To Java
    Replies: 5
    Last Post: 11-27-2009, 11:38 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
  •