2

Trying to use switch in strings by first coverting string into char and then apply switch but still didnt done it....here is my code..

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

class HappyBirthday {

    public static void main(String[] args) throws IOException {
        String Month;
        char[] Months = Month.toCharArray();
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter your month.");

        Month = JOptionPane.showInputDialog("enter month");
        String month1 = { "January", "feb"};
        char[] month2 = month1.toCharArray();

        // String s=month1.equals(Month);
        // System.out.print(month2Array[0]);
        switch (month2) {

        case 0:
            System.out.println("kool");
            break;

        case 1:
            System.out.println("not kool");
            break;
        default:
        }
    }
}
/**
 * if (month1[1].equals(Month)) System.out.println("kool"); else
 * if(month1[0].equals(Month)) System.out.println("kooooooooooooool"); else
 * System.out.println("Big kooooool");
 **/
3
  • 1
    I was pretty close to downvoting the question just because the code formatting is a desaster! Please, for the next time, take a minute an clean it up before posting. Commented May 29, 2010 at 9:59
  • (virtual +1 for skaffman for cleaning up ;-) ) Commented May 29, 2010 at 10:02
  • using string in switch is part of java7 Commented May 29, 2010 at 10:16

4 Answers 4

4

Have a look at this excellent article on the subject.

Sign up to request clarification or add additional context in comments.

Comments

1

Currently, you can not switch on a String. The language specification is clear on what you can switch on:

JLS 14.11 The switch statement

SwitchStatement:
      switch ( Expression ) SwitchBlock

The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type, or a compile-time error occurs.

Depending on what you want to do, you can switch on each char of a String:

    String s = "Coffee, tea, or me?";
    int vowelCount = 0;
    int punctuationCount = 0;
    int otherCount = 0;
    for (char letter : s.toUpperCase().toCharArray()) {
        switch (letter) {
            case 'A': 
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                vowelCount++;
                break;
            case ',':
            case '.':
            case '?':
                punctuationCount++;
                break;
            default:
                otherCount++;
        }
    }
    System.out.printf("%d vowels, %d punctuations, %d others",
        vowelCount, punctuationCount, otherCount
    ); // prints "7 vowels, 3 punctuations, 9 others"

Comments

1

Note that switching on strings will be supported in Java 7.

1 Comment

According to the changelogs, this code has made it into the codebase. bugs.sun.com/view_bug.do?bug_id=6827009
0

You can't switch on a char[] type. Switch on char[0] and use case 'J': and so on (although - because some months start with the same letter, the algorithm would be sub-optimal)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.