• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch Statement and a String

 
Naveen R R
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
sql = "INSERT INTO PROJOWNER.DEPT_ROLE_ASSN
(ROL_ROLE_ID, DEP_COST_CTR) VALUES (?, ?)";
input.addElement(roleId);
input.addElement(costCtr);
dba.executeUpdate(sql, input);
break;
case TEAM:
......
......
Thank you,
Naveen
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() )
Bill
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't forget that you can always use an if-chain:
 
Naveen R R
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You.
Naveen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic