This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Servlets and the fly likes Switch Statement and a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Switch Statement and a String" Watch "Switch Statement and a String" New topic

Switch Statement and a String

Naveen R R

Joined: May 04, 2001
Posts: 20
Could anyone please help me with this? How do I get a switch statement and a string together?
Please take a look at the code below:
public class AssignRoleParams {
private String roleLevel;
public void setRoleLevel(String newRoleLevel) {
roleLevel = newRoleLevel;
public String getRoleLevel() {
return roleLevel;
public class ProjectOfficeSecurity {
public void assignRole(AssignRoleParams roleAssign) {
db.DBAccess dba = null;
String sql = null;
try {
String roleId = roleAssign.getRoleId();
String costCtr = roleAssign.getCostCtr();

dba = new db.DBAccess(connection);
java.util.Vector input = new java.util.Vector();

//ERROR - case constant must be a char, byte, short,
or int instead of java.lang.String
switch (roleAssign.getRoleLevel()) {
case DEPT:
dba.executeUpdate(sql, input);
case TEAM:
Thank you,
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12893
Here is what I have done to select a case according to a String. You have to set up a Hashtable where each key is a String you want recognized, and the object stored is an Integer that has a unique numeric value. Then you can just
switch( ((Integer)table.get( keystring )).intValue() )
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
And don't forget that you can always use an if-chain:

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Naveen R R

Joined: May 04, 2001
Posts: 20
Thank You.
I agree. Here's the link:
subject: Switch Statement and a String