File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes String in switch case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String in switch case" Watch "String in switch case" New topic

String in switch case

Asvin Kumar

Joined: Mar 01, 2010
Posts: 13
I want to use a string in switch case and I get the following error,

[javac] incompatible types
[javac] found : java.lang.String
[javac] required: int
[javac] switch(type)

any help??
Shinelin Samuel
Ranch Hand

Joined: Mar 01, 2010
Posts: 55
You cant use String in switch case, allows types are int and char.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19656

Actually, int and everything that is assignable to int. That includes char, short and byte. Since Java 5.0 enums can also be used in switch statements, and Java 7 may (finally?) introduce the switch statement for Strings. It has been discussed, but I don't know what the status on that is.

How To Ask Questions How To Answer Questions
Asvin Kumar

Joined: Mar 01, 2010
Posts: 13
Thanks for your replies...
I agree. Here's the link:
subject: String in switch case
Similar Threads
Switch and Assertion
switch case
Case switch
String support for switch case statement
switch doubt..