Switch on Strings  
Author Message
fb





PostPosted: 2006-1-14 8:32:00 Top

java-programmer, Switch on Strings Hello, I have code that looks like this:

switch(myColor){
case "blue":
{
this.setForeground(Color.blue);
break;
}
case "red":
{
this.setForeground(Color.red);
break;
}
case "green":
{
this.setForeground(Color.green);
break;
}
default:
{
this.setForeground(Color.blue);
}
}

myColor is a String being read in from an HTML file PARAM line.
The above code doesn't work, because switch doesn't work with strings.
So I was wondering how else I can do this...

ttyl
 
Andrew McDonagh





PostPosted: 2006-1-14 8:55:00 Top

java-programmer >> Switch on Strings fb wrote:
> Hello, I have code that looks like this:
>

snipped switch statement

>
> myColor is a String being read in from an HTML file PARAM line.
> The above code doesn't work, because switch doesn't work with strings.
> So I was wondering how else I can do this...
>
> ttyl

One of many ways....

First off, create a Map of the strings and their corresponding Color
OBJECTs (in case you don't know, when you do 'Color.blue' what is
actually happening is a Color object is returned and that object
represents the blue colour)


Map colorsToUse = new HashMap();
colorsToUse.put("blue", Color.blue);
colorsToUse.put("red", Color.red);
colorsToUse.put("green", Color.green);



Then in the method that is handling the PARAM line, we simply need to
see if the PARAM String value is found in the map, if so, use it, if not
default to blue.....



public void someMethodHandlingHTMLfile(String colorParamFromHTML) {

Color actualColor = Color.blue; // defaulting to blue

if (colorsToUse.containsKey(colorParamFromHTML)
actualColor = (Color)colorsToUse.get(colorParamFromHtml);

this.setForeground(actualColor);
}


The beauty here, is that when you need to handle additional colors, you
only have to add them to the map and the rest of the code won't need to
change.
 
VisionSet





PostPosted: 2006-1-14 9:02:00 Top

java-programmer >> Switch on Strings
"fb" <email***@***.com> wrote in message news:mEXxf.107794$tl.93013@pd7tw3no...

>
> myColor is a String being read in from an HTML file PARAM line.
> The above code doesn't work, because switch doesn't work with strings.
> So I was wondering how else I can do this...
>

switch supports primitive none floating point types only
direct translation is hardly more verbose:

if("blue".equals(myColor)) this.setForeground(Color.blue);
else if("red".equals(myColor)) this.setForeground(Color.red);

else this.setForeground(Color.blue);

But better is:

You want to Map a String to a Color constant
So use a Map with those mappings

Map<String, Integer> colorMap = new HashMap<String, Integer>();
colorMap.put("red", Color.red);
colorMap.put("blue", Color.blue);

if (colorMap.contains(myColor)) {
setForeground(colorMap.get(myColor));
}



 
 
VisionSet





PostPosted: 2006-1-14 9:04:00 Top

java-programmer >> Switch on Strings
"VisionSet" <email***@***.com> wrote in message
news:d4Yxf.35028$email***@***.com...

> So use a Map with those mappings

okay, doh Color is an erm... Color, not an int/Integer


Map<String, Color> colorMap = new HashMap<String, Color>();


 
 
Roedy Green





PostPosted: 2006-1-14 11:47:00 Top

java-programmer >> Switch on Strings On Sat, 14 Jan 2006 00:32:18 GMT, fb <email***@***.com> wrote, quoted or
indirectly quoted someone who said :

>
>myColor is a String being read in from an HTML file PARAM line.
>The above code doesn't work, because switch doesn't work with strings.
>So I was wondering how else I can do this...

you do it with enums. See http://mindprod.com/jgloss/enum.html

You convert your switch string to an enum with valueOf and use enum
constants instead of strings on your case labels.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Roedy Green





PostPosted: 2006-1-14 11:49:00 Top

java-programmer >> Switch on Strings On Sat, 14 Jan 2006 00:32:18 GMT, fb <email***@***.com> wrote, quoted or
indirectly quoted someone who said :

>switch(myColor){
> case "blue":
> {
> this.setForeground(Color.blue);
> break;
> }
> case "red":
> {

The code can look like this wit enums with methods.


setBackground( colourScheme.getColor() );
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
fb





PostPosted: 2006-1-21 10:07:00 Top

java-programmer >> Switch on Strings fb wrote:
> Hello, I have code that looks like this:
>
<SNIP Code>
Thanks for your help everyone...Got it working.