1
public class SwitchExampleString
{
    public static void main(String args[])
    {
        String choice;
        switch(args)
        {
            case "day1" :
                choice="Sunday";
                System.out.println(choice);
                break;
            case "day2" :
                choice="Monday";
                System.out.println(choice);
                break;
            case "day3" :
                choice="Tuesday";
                System.out.println(choice);
                break;
            case "day4" :
                choice="Wednesday";
                System.out.println(choice);
                break;
            case "day5" :
                choice="Thursday";
                System.out.println(choice);
                break;
            case "day6" :
                choice="Friday";
                System.out.println(choice);
                break;
            case "day7" :
                choice="Saturday";
                System.out.println(choice);
                break;
            default :
                System.out.println("Wrong choice");
        }
    }
}

Can anyone help me,i want to know how to use a string inside a switch(). The above shown is the program i had done so far. But it is showing errors. The java version which i have installed is jdk6.

david
  • 3,225
  • 9
  • 30
  • 43
arjun
  • 23
  • 5

8 Answers8

5

The problem is you are switching on a String array not a String...

switch(args[0])

Would work - given you are using JDK7... And there is an argument supplied to your program - otherwise you'd get a nice ArrayOutOfBoundsException...

ppeterka
  • 20,583
  • 6
  • 63
  • 78
0

Switch with String was introduced in JDK version 7 . So you need to upgrade to jdk7.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Check your choice or args[0]

public class SwitchExampleString
{

public static void main(String args[])

{
    String choice="day1";
    switch(choice)
    {
case "day1" :
    choice="Sunday";
        System.out.println(choice);
    break;
case "day2" :
    choice="Monday";
        System.out.println(choice);
    break;
case "day3" :
    choice="Tuesday";
        System.out.println(choice);
    break;
case "day4" :
    choice="Wednesday";
        System.out.println(choice);
    break;
case "day5" :
    choice="Thursday";
        System.out.println(choice);
    break;
case "day6" :
    choice="Friday";
        System.out.println(choice);
    break;
case "day7" :
    choice="Saturday";
        System.out.println(choice);
    break;
    default :
System.out.println("Wrong choice");
    }
}
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
0

You'd need to do it one string from the arguments, not all of them.

switch (args[0]) {
    case "day1" :
       //...

Practically, if you're decoding days you should put that logic in a parsing class -- and probably wouldn't encode them as dayN either. Consider case-insensitivity, as well:

Hopefully that's just your test usecase.. not real design.

With case-insensitivity (forcing to lowercase):

switch (args[0].toLowerCase()) {
    case "day1" :
       //...
Thomas W
  • 13,940
  • 4
  • 58
  • 76
0

In the JDK 7 release, you can use a String object in the expression of a switch statement:

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
     String typeOfDay;
     switch (dayOfWeekArg) {
         case "Monday":
             typeOfDay = "Start of work week";
             break;
         case "Tuesday":
         case "Wednesday":
         case "Thursday":
             typeOfDay = "Midweek";
             break;
         case "Friday":
             typeOfDay = "End of work week";
             break;
         case "Saturday":
         case "Sunday":
             typeOfDay = "Weekend";
             break;
         default:
             throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
     }
     return typeOfDay;
}

The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.equals method; consequently, the comparison of String objects in switch statements is case sensitive. The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.

for more info click here

Ali Ahmad
  • 1,351
  • 9
  • 11
0

strings in switch statements which is recently added feature in Java SE 7.

Using JDK 7, you can pass string as expression in switch statement.

Chirag Kathiriya
  • 427
  • 5
  • 14
0

You can use switch with String also, however you're trying to use a String array. Pick one argument which you want and use it as the String in the switch.

For example:

String yourChoice = args[0];

switch(yourChoice)
{
    case "something":
        System.out.print("this");
    case "somethingElse":
        System.out.print("that");
}

Also you have to use Java7 or newer and you have to ensure that args[0] isn't null - you have to input an argument when you run your JAR.

Dropout
  • 13,653
  • 10
  • 56
  • 109
0

search on project coin, you will get more info on this. or you can goto simply here: http://java.dzone.com/articles/java-7-%E2%80%93-project-coin-feature dzone, tutorial.

and you can download java 7 from here:http://www.oracle.com/technetwork/java/javase/downloads/index.html

A N M Bazlur Rahman
  • 2,280
  • 6
  • 38
  • 51