How to use string object in switch case
/**
* In Java SE 7 and later, you can use a String object in the switch statement’s expression
* Here is a small example
* If the string is null , it will throw a nullPointerException
* This is not the Best Java Programing Blog, I am just a learner in java
* Download jdk1.7 or above
*/
* In Java SE 7 and later, you can use a String object in the switch statement’s expression
* Here is a small example
* If the string is null , it will throw a nullPointerException
* This is not the Best Java Programing Blog, I am just a learner in java
* Download jdk1.7 or above
*/
package com.nick.Switch;
import java.util.Scanner;
/**
* @author Oscar
*
*/
public class SwitchCaseString {
* @author Oscar
*
*/
public class SwitchCaseString {
/**
* @param mon : denotes the first 3 char of month entered by the user
* @return month name
*/
* @param mon : denotes the first 3 char of month entered by the user
* @return month name
*/
private String getMonthName(String mon) {
String month = null;
switch (mon.toUpperCase()) {
case “JAN”: month =” its January “;
break;
case “FEB”: month =” its February “;
break;
case “MAR”: month =” its March “;
break;
case “APR”: month =” its April “;
break;
case “MAY”: month =” its May”;
break;
case “JUN”: month =” its June “;
break;
case “JUL”: month =” its Jully “;
break;
case “AUG”: month =” its August “;
break;
case “SEP”: month =” its September “;
break;
case “OCT”: month =” its October “;
break;
case “NOV”: month =” its November “;
break;
case “DEC”: month =” its December “;
break;
default:
month =” INVALID “;
break;
}
case “JAN”: month =” its January “;
break;
case “FEB”: month =” its February “;
break;
case “MAR”: month =” its March “;
break;
case “APR”: month =” its April “;
break;
case “MAY”: month =” its May”;
break;
case “JUN”: month =” its June “;
break;
case “JUL”: month =” its Jully “;
break;
case “AUG”: month =” its August “;
break;
case “SEP”: month =” its September “;
break;
case “OCT”: month =” its October “;
break;
case “NOV”: month =” its November “;
break;
case “DEC”: month =” its December “;
break;
default:
month =” INVALID “;
break;
}
return month;
}
}
public static void main(String[] akshara) {
SwitchCaseString instance = new SwitchCaseString();
System.out.println(” Enter the first 3 character of the month “);
System.out.println(instance.getMonthName(new Scanner(System.in).next()));
SwitchCaseString instance = new SwitchCaseString();
System.out.println(” Enter the first 3 character of the month “);
System.out.println(instance.getMonthName(new Scanner(System.in).next()));
}
}
Switch case example for practical question ( lab exams)
Switch case example for practical question ( lab exams)
Thanking you
No comments:
Post a Comment
Your feedback may help others !!!