Java Program to Implement Switch Statement on Strings

«
»
This is a Java Program to Use Strings in Switch Statements.

Enter the string as input. Now we match the given string with different cases and then generate the output accordingly.

Here is the source code of the Java Program to Use Strings in Switch Statements. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Strings_Switch
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         String week;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter choice:");
  9.         week = s.nextLine();
  10.         switch (week) 
  11.         {
  12.             case "Monday":
  13.             System.out.print("Day: Monday");
  14.             break;    
  15.  
  16.             case "Tuesday":
  17.             System.out.print("Day: Tuesday");
  18.             break;
  19.  
  20.             case "Wednesday":
  21.             System.out.print("Day: Wednesday");
  22.             break;
  23.  
  24.             case "Thursday":
  25.             System.out.print("Day: Thursday");
  26.             break;
  27.  
  28.             case "Friday":
  29.             System.out.print("Day: Friday");
  30.             break;
  31.  
  32.             case "Saturday":
  33.             System.out.print("Day: Saturday");    
  34.             break;
  35.  
  36.             case "Sunday":
  37.             System.out.print("Day: Sunday");   
  38.             break;    
  39.         }
  40.     }
  41. }

Output:

$ javac Strings_Switch.java
$ java Strings_Switch
 
Enter choice:Saturday
Day: Saturday

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.