Home » Java solved programs

Java - Switch Case Statement Example Code for String

In this code we will learn how to use switch case statement with string. In this program we will read color name Red, Green or Blue and print entered color name using switch case statement.

Java Code - Switch Case Statement Example Code for String

//Java - Switch Case Statement Example Code - Print Weekdays.
import java.util.Scanner;
 
public class SwitchCaseExample{ 
      public static void main(String args[]){
          int day;
          Scanner SC=new Scanner(System.in);
           
          System.out.print("Enter a weekday number (0 to 6): ");
          day=SC.nextInt();
           
          if(day<0 || day>6){
              System.out.println("Invalid weekday number.");
              System.exit(0);
          }
           
          //print weekday name according to give value
          switch(day){
                case 0: 
                    System.out.println("Sunday");
                    break;
                case 1: 
                    System.out.println("Monday");
                    break;
                case 2: 
                    System.out.println("Tuesday");
                    break;
                case 3: 
                    System.out.println("Wednesday");
                    break;
                case 4: 
                    System.out.println("Thursday");
                    break;
                case 5: 
                    System.out.println("Friday");
                    break;
                case 6: 
                    System.out.println("Saturday");
                    break;                  
                default:
                    System.out.println("Invalid");
                    break;                  
          }
           
      }
}

Output


    First Run:
    Enter color name (Red, Green, Blue): Red
    You've selected Red Color.

    Second Run:
    Enter color name (Red, Green, Blue): Pink
    You've selected Invalid Color.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.