Java doesn't allow you to use anything other than integer (or castable
to integer) values for switch statements, so strings can't be sued.

With a switch that small, I recommend a series of if statements

String s=department.trim().toUpperCase();
if (s.equals("FAO")) rate=0.09;
else if (s.equals("CAO")) rate=0.19;
else if (s.equals("RES")) rate=0.39;
else  rate=0.09;

This will do at most 3 string comparisons, which isn't all that bad.

Assuming you are just mapping values to values, you might want to use a
map/hashtable/properties object...

Example:  
// scope this in the application scope
java.util.Hashtable values=new java.util.Hashtable();
values.put("CAO",new Double(0.19));
values.put("RES",new Double(0.39));
values.put("DEFAULT",new Double(0.09));

Then you can do Double d=(Double)
values.get(department.trim().toUpperCase()) to get the value.

This is usually better for larger cases, where lots of if statements
would take longer than a single hash lookup

----
Then there's an alternative way to use switches if you have more code
than just an assignment to make.

First convert all your string values to integers via a map, and switch
on the integers....

I would do this (so it's easy to read, like this)
Static final int FAO=1, CAO=2, RES=3;

Hashtable table=new Hashtable();
Table.put("FAO",new Integer(FAO));
Table.put("CAO",new Integer(CAO));
Table.put("RES",new Integer(RES));

int switchValue=-1;
Integer d=(Integer ) table.get(department.trim().toUpperCase()) 
if (d!=null) swtichValue=d.intValue();

switch(switchValue){
        case FAO:
        ...
        break;
        ...
        etc...
}


-----Original Message-----
From: Ciliotta, Mario [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, December 29, 2004 9:55 AM
To: CF-Talk
Subject: Question for Java people on this list: Converting a CF app to
JS P

Hi,

I was wondering if someone on this might have experience using the
SWITCH statement in JSP.

I am in the process of converting a CF app to JSP (Do not ask why --
company standards for the future) and in the CF page I make use of the
<CFSWITCH> and I looked into JSP/Java and there is also a switch
statement but I cannot get it to work correctly.

Here is a CF example:

<cfswitch expression="#Trim(Department)#"> 
   <cfcase value="FAO"> 
      <cfset rate=".09"> 
   </cfcase> 
   <cfcase value="CAO"> 
      <cfset rate=".19"> 
   </cfcase>
   <cfcase value="RES"> 
       <cfset rate=".39"> 
   </cfcase> 
   <cfdefaultcase> 
      <cfset rate=".09"> 
   </cfdefaultcase>
</cfswitch> 

Does anyone know if this can be done with the SWITCh statement in JSP.
Can switch be used on strings --- I have been told yes and no by
different people but I cannot get past the problem.

Thanks
Mario





~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Special thanks to the CF Community Suite Gold Sponsor - CFHosting.net
http://www.cfhosting.net

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188928
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to