Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: switch with Enums

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default switch with Enums

    if im not mistaken , i saw a post in this forum that shows how to make a switch statement using char
    and i saw some of the parts of the program was declared as enum..

    the cases inside the swithc was character.. can anyone help me find it...? please... tnx in advance...


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: switch with character

    I'm confused...
    public class Switch {
     
    	public static void main(String[] args) {
    		Scanner sin = new Scanner(System.in);
    		char x = sin.next().charAt(0);
    		switch(x){
    			case 'a': System.out.println("WOW 'a'"); break;
    			default: System.out.println("try 'a' ;)"); break;
    		}
    	}
    }

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    chronoz13 (October 7th, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: switch with character

    no chris.. i saw something like that in switch statement and some of the variables were enum.. i dont remember exactly the code.. but i think this is where i saw that code..

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: switch with character

    public class SomeClass
    {
         public static enum Days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
     
         public static void main(String[] args)
         {
              Days a = Days.SUNDAY;
              switch(a)
              {
                   case Days.SUNDAY:
                        System.out.println("Today is sunday");
                        break;
                   case Days.Saturday:
                        System.out.println("Today is saturday");
                        break;
                   default:
                        System.out.println("Today is some weekday");
                   }
         }
    }

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 7th, 2009)

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: switch with character

    YAH its exactly like that!!!! tnx helloworld

  8. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: switch with character

    I take it you wrote that from the top of your head helloworld?

    // Json

  9. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: switch with character

    there an error here... cant figure it out..

    public class SomeClass
    {
         public static enum Days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
     
         public static void main(String[] args)
         {
              Days a = Days.SUNDAY;
              switch(a)
              {
                   case Days.SUNDAY:  
                        System.out.println("Today is sunday");
                        break;
                   case Days.Saturday:  
                        System.out.println("Today is saturday");
                        break;
                   default:
                        System.out.println("Today is some weekday");
                   }
         }
    }

    case Days.SUNDAY:  //<< an enum switch case label must be the unqualified name of an enumeration constant

    case Days.Saturday: //<< duplicate case label, an enum switch case label must be the unqualified name of an enumeration

  10. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: switch with character

    Thats what I was commenting on above as well.

    When using enums in switch statments you are not allowed to use the fully qualified name of the enum values.

    So the code block above should look like this.

    public class SomeClass
    {
         public static enum Days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
     
         public static void main(String[] args)
         {
              Days a = Days.SUNDAY;
              switch(a)
              {
                   case [b]SUNDAY[/b]:  
                        System.out.println("Today is sunday");
                        break;
                   case [b]SATURDAY[/b]:  
                        System.out.println("Today is saturday");
                        break;
                   default:
                        System.out.println("Today is some weekday");
                   }
         }
    }

    // Json

  11. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (October 7th, 2009)

  12. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: switch with character

    oh no! i forgot to ask how to make an input for it... big sorry for this..
    Last edited by chronoz13; October 7th, 2009 at 09:14 AM.

  13. #10
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: switch with character

    Make an input?

        Days days = Days.valueOf("SUNDAY");

    Make sure the string you pass into the valueOf method is the same case as the enums you've reclared.

    // Json

  14. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (October 7th, 2009)

  15. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: switch with character

    I'm not sure if any of you have noticed but if you look at the bottom of this thread, there is a 'Similar Threads' box. I'm sure this will come in handy sometimes!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  16. The Following User Says Thank You to JavaPF For This Useful Post:

    chronoz13 (October 7th, 2009)

  17. #12
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: switch with character

    weee tnx for all your help!! yehey!

  18. #13
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Smile Re: switch with character

    oh men.... i didnt notice it sir Java... anyway big thanks to you!!

  19. #14
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: switch with character

    so i was ryt what im looking for was posted in some of the threads here.. i just coudlnt find it .. i was looking at the tutorials but i didnt find it... haha i forgot that it was sir json's post...

  20. #15
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: switch with character

    A lot of the code I post is off the top of my head. It takes too much time to open up eclipse and type the same thing when I could just do it here. Yeah, I forgot you don't need the full quantifier for enums in switch statements. You do need them in any other conditionals, though.

  21. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 7th, 2009)

  22. #16
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: switch with character

    Damn switches to hell! They always were a bizzare feature in programming languages imo.

  23. #17
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Exclamation Re: switch with Enums

    forgive me but i have so many questions.. to avoid confusing,i will state it numerically.

    1.) why are the enums in these switch programs declared as public, not private? is the logic in constant variable/data members apply into it? (and accessibility ), so it can be used by other programs if I WANT IT to?

    for example:
    .DAY_OF_WEEK of GregorianCalendar class. it can be used by other programs although its constant.

    OR enums should be declared as public?
    (hope you understand what i want to know).
    --SOLVED (no.1);


    ---------------
    2.) when will be the
    default:
    executed in this switch? I made an input value as i asked,(according to the algorithm that sir Json gave me, THAT I HAVE TO INPUT THE EXACT CASE of the enum variable).when i entered a String thats differs form the case of the enum variables ,There's an exception error, theres no 'default' ..even the program that sir Json made in tutorials,the switch will generate a random switch enum value...i run it many times but i dont see the execution of the default part.. why is it?

    3.)I cant override the code of .value() method, why would i like to?, I want to see how does the string value CONVERTED into an enum type just by passing it into the parameter of this method. why should I implement the .valueOf() method, Tthe program is still running fine withour implementing it.?
    ---------------
    4.) last and for most. when i was coding this (editing this code) i've notice that the logic of these program can be made by just using if-then-else structure...

    QUESTION: is my program efficient with this kind of structure? why did i ask this? this question came up in my mind when i was coding this part,
    the 'if-then' part(its no yet finished) but before i end this part.. I have to ask these questions first.
     String keyIn,
                    enumFormat = "";
     
             System.out.print("Enter the day: ");
             keyIn = sc.next();
     
             if (keyIn.equals("Sat") || (keyIn.equals("Saturday")
                     || (keyIn.equals("SAT")) || keyIn.equals("saturday"))
                     || (keyIn.equals("sat"))) {
     
                 keyIn = "SATURDAY";
                 enumFormat = keyIn;
             }
     
             Days days = Days.valueOf(enumFormat);
     
             switch(days) {
    ]

    you can notice that the logic of the 'if-then' part.is that no matter what format i will input, it will still send an exact the same CASE of the enum as String in the .value() method..

    so far regarding with editing this code with my simple logic here what I got so far.. please check any INEFFICIENCIES ...

    public class SwitchSample3_EnumType_UF {
     
         private static Scanner sc = new Scanner(System.in);
     
         private static enum Days {sun, sunday, SUNDAY, mon, monday, MONDAY, tues,
         tuesday, TUESDAY, wed, wednesday, WEDNESDAY, thurs, thursday, THURSDAY,
         fri, friday, FRIDAY, sat, saturday, SATURDAY}
     
         public static void main(String[] args) {
     
             String keyIn,
                    enumFormat = "";
     
             System.out.print("Enter the day: ");
             keyIn = sc.next();
     
             Days days = Days.valueOf(keyIn);
     
             switch(days) {
     
                 case SUNDAY:
                 case sunday:
                 case sun:
     
                     System.out.println("Today is Sunday");
                     break;
     
                 case MONDAY:
                 case monday:
                 case mon:
     
                     System.out.println("Today is Monday");
                     break;
     
                 case TUESDAY:
                 case tuesday:
                 case tues:
     
                     System.out.println("Today is Tuesday");
                     break;
     
                 case WEDNESDAY:
                 case wednesday:
                 case wed:
     
                     System.out.println("Today is Wednesday");
                     break;
     
                 case THURSDAY:
                 case thursday:
                 case thurs:
     
                     System.out.println("Today is Thursday");
                     break;
     
                 case FRIDAY:
                 case friday:
                 case fri:
     
                     System.out.println("Today is Friday");
                     break;
     
                 case SATURDAY:
                 case saturday:
                 case sat:
     
                     System.out.println("Today is Saturday!");
                     break;
     
                 default:
     
                     System.out.println("Say What? Make Your Own Calendar!");
                     break;
             }
         }
    }
    Last edited by helloworld922; October 8th, 2009 at 07:54 PM.

  24. #18
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: switch with Enums

    The default block is equivalent to the "else" part of an if-else statement. If the given test is false for all the cases, then the default section will be executed.

    private static enum Days {sun, sunday, SUNDAY, mon, monday, MONDAY, tues,
         tuesday, TUESDAY, wed, wednesday, WEDNESDAY, thurs, thursday, THURSDAY,
         fri, friday, FRIDAY, sat, saturday, SATURDAY}

    I wouldn't declare so many different day enums, in Java they are all different (also in C/C++, but there is a way to get around this in these languages. Java has them as always different)

    Also, you're calling a Days.valueOf(String day) without defining that method inside your enum. Remember, the computer isn't that smart, and you must tell it what you want when you call that method

    In the olden days, enum stood for enumeration, which is basically a fancy way to say counting/numbering. Each enum value was given an integer value, starting from 0, and counted up. However, you were allowed to assign values to these enums. In Java, this has changed. For the better or for worse is personal opinion. Java's enums are special values and have no integer/number equivalent (unless you define some special conversion method). They also have no String equivalent."SUNDAY" != Days.SUNDAY!!

    In this case, it doesn't matter if you declared the enum public, protected, or private because you used it completely within one class. However, if you want other classes to be able to inherit the enum and use it, it can't be private, and if you want any class to be able to use it, the enum can't be protected or private.

    Enums are kind of in a special group like interfaces are. They can behave like classes, but also have some special properties/limits put on them.

    edit:

    Hmm, it turns out there's an Enum wrapper class, and that apparently does have the valueOf method defined I still wouldn't create so many enums and would over-write the valueOf method and have "Sunday", "sun", and "SUNDAY" all map to the same enum value.

    public enum Days
    {
         SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;
     
         public Days valueOf(String str)
         {
              if (str.toLowerCase().equals("sunday") || str.toLowerCase().equals("sun"))
              {
                   return SUNDAY;
              }
              //... etc
         }
    }
    Last edited by helloworld922; October 8th, 2009 at 09:04 PM.

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. Convert from 'C' to "java" (switch)
    By chronoz13 in forum Loops & Control Statements
    Replies: 7
    Last Post: October 6th, 2009, 08:20 PM
  3. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM