aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Switch Statement and a String" Watch "Switch Statement and a String" New topic
Author

Switch Statement and a String

Naveen R R
Greenhorn

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:
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

Joined: Mar 22, 2000
Posts: 12682
    
    5
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

Java Resources at www.wbrogden.com
Frank Carver
Sheriff

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


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

Joined: May 04, 2001
Posts: 20
Thank You.
Naveen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch Statement and a String
 
Similar Threads
Conversion of int to String in a Vector
Servlet Request (Integer)
Assertion Question
Arrays
Can't use Optimistic Locking