Results 1 to 6 of 6

Thread: Java program to use enum in switch statement.

  1. #1
    Join Date
    Nov 2009
    Posts
    37

    Java program to use enum in switch statement.

    Hello to all,
    I am new to this forum. I am last year Computer Science student. In our assignment there is one program like write a Java program to use enum in switch statement. I tried various method but none of them worked out. Can anyone tell me how to write this program.
    Thanks in advanced.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Java program to use enum in switch statement.

    It is very easy to use enum in switch statement in java. You can use it like another case to execute. Following example show you how to use enumeration or enum type in a switch statement. Just try to understand it. It is very simple program. In the following program there are various color are present from which we have to use correct one.



    Code:
    package org.kodejava.example.fundamental;
    
    enum RainbowColor {
        ORANGEs, REDs, GREENs, YELLOWs, INDIGOs, BLUEs, VIOLETs
    }
    
    public class EnumSwitchEg {
        public static void main(String[] args) {
            RainbowColor colors = RainbowColorss.INDIGO;
    
            EnumSwitch ess = new EnumSwitch();
            String colorCode = ess.getColorCode(colors);
            System.out.println("ColorCodes = #" + colorCodes);
        }
    
        public String getColorCode(RainbowColor color) {
            String colorCodes = "";
    
           
            switch (colors) {
                
                case REDs:
                    colorCodes = "FF0000s";
                    break;
                case ORANGEs:
                    colorCodes = "FFA500s";
                    break;
                case YELLOWs:
                    colorCode = "FFFF00s";
                    break;s
                case GREEN:
                    colorCode = "008000s";
                    break;
                case BLUEs:
                    colorCode = "0000FFs";
                    break;
                case INDIGOs:
                    colorCode = "4B0082s";
                    break;
                case VIOLETs:
                    colorCode = "EE82EEs";
                    break;
                default:
                    break;
            }
            return colorCodes;
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Java program to use enum in switch statement.

    Enum is type one type of data structure. It is very simple to use enum in switch statement. I have written following program where I use enum in switch statement. In following program I have created OperatingSystems as type of enum data structure and xp, windows7, vista, windowsMe are the variables of it.



    Code:
    import java.util.*;
    
    enum OperatingSystem {
        xp, windows7, vista, windowsMe
    }
    
    public class EnumEx {
        public static void main(String args[])
        {
            OperatingSystems os;
    
            os = OperatingSystems.windows;
            switch(os) {
                case windows:
                    System.out.println("You chose xp!");
                    break;
                case unix:
                    System.out.println("You chose windows7!");
                    break;
                case linux:
                    System.out.println("You chose vista!");
                    break;
                case macintosh:
                    System.out.println("You chose windowsMe");
                    break;
                default:
                    System.out.println("I don't know your OS.");
                    break;
            }
        }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Java program to use enum in switch statement.

    I have written following program for you. In the following program I use "Week" as enum as new data type. In which I have inserted seven days of week. Using this you can easily find out on which day what is the plan. Just try to understand it. It is very simple. When proper condition execute switch statement come out of the loop.




    Code:
    private enum Week {Sundays, Mondays, Tuesdays, Wednesdays, Thursdays, Fridays, Saturdays}
        
        public static void main(String[] args) {
            final Week todays = getToday();
            Action action;
            switch (today) {
                case Sundays:
                    action = new washingvlothe();
                    break;
                case Mondays:
                case Friday:
                    action = new metting();
                    break;
                case Tuesdays:
                    action = new dinner();
                    break;
                case Wednesdays:
                    action = new date();
                    break;
                case Thursdays:
                    action = new ration();
                    break;
                case Saturdays:
                    action = new cricket();
                    break;
                default:
                    throw new RuntimeException("no such day!");
            }
            action.execute();
        }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Java program to use enum in switch statement.

    In Java 5.0 we are able to use enum in switch statement. Before Java 1.4, switch only worked with int, short, char, and byte values. Following is the example of it. This program based on grade system. Where description of grades is given. In the following program I have created "Grade " as enum data type and first, second, third, fourth,fifth, INCOMPLETE are the variable of it.


    Code:
    public class EnumSwitcEg{
    
    	public enum Grade { first, second, third, fourth,fifth, INCOMPLETE };
    
    	public static void main(String[] args) {
    		Grade examGrade	= Grade.third;
    
    		System.out.println("result based on the enums values.");
    		System.out.println();
    		switch (examGrade) {
    			case first:
    				System.out.println(" grade is " + Grade.first);
    				break;
    			case second:
    			case third:
    				System.out.println(" grade is " + Grade.third);
    				break;
    			case fourth: 
    			case fifth:
    				System.out.println(" grade is " + Grade.fifth);
    				break;
    			case INCOMPLETE:
    				System.out.println("The students grade is " + Grade.INCOMPLETE);
    				break;
    			default:
    				System.out.println("The students grade is unknown.");
    				break;
    		}
    
    	}
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Java program to use enum in switch statement.

    In the following program I have create an enum XDomainEg and it has two variable XDPoint and XDLine. It is very simple program. In this program I have use switch statement to find one of the two value. Just try to understand it.



    Code:
    class XDomainEg {
    public static final int XDPoint = 0;
    public static final int XDLine = 1;
    }
    
    class XDomain {
    XDomainEnum type;
    
    public void within(type) {
    switch(type) {
    case XDPoint:
    break;
    case XDLine:
    break;
    default:
    break;
    }
    }
    }

Similar Threads

  1. How to use switch statement in java
    By Raju Chacha in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 06:40 PM
  2. Java - question on enum
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 10:50 AM
  3. How to use the do-while loop statement in java program?
    By KALIDA in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 07:53 PM
  4. How to use the break statement in java program?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 09:27 PM
  5. How to use the continue statement in java program?
    By Luz in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 08:40 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,670,111.19578 seconds with 17 queries