Wednesday, 18 November 2015

Java Tutorial : Java switch statement(String)

SwitchDemo.java
class SwitchDemo
{
    public static void main(String[] args)
    {

        String month = "march";
        int monthNumber = 0;

        /*
         * In Java SE 7 and later, you can use a String object in the switch
         * statement's expression.
         */
        switch (month)
        {
        case "january":
            monthNumber = 1;
            break;
        case "february":
            monthNumber = 2;
            break;
        case "march":
            monthNumber = 3;
            break;
        case "april":
            monthNumber = 4;
            break;
        case "may":
            monthNumber = 5;
            break;
        case "june":
            monthNumber = 6;
            break;
        case "july":
            monthNumber = 7;
            break;
        case "august":
            monthNumber = 8;
            break;
        case "september":
            monthNumber = 9;
            break;
        case "october":
            monthNumber = 10;
            break;
        case "november":
            monthNumber = 11;
            break;
        case "december":
            monthNumber = 12;
            break;
        default:
            monthNumber = 0;
            break;
        }

        System.out.println("month number : " + monthNumber);

    }
}
Output
month number : 3
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ControlFlowDemoSwitch-String-App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ControlFlowDemoSwitch-String-App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/08783475a968761a2cf4b3fc7275842653545b35/BasicJava/ControlFlowDemoSwitch-String-App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment