ADVERTISEMENT
ADVERTISEMENT

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.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.