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 - 14 Jan 2006 00:54 GMT
> 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 - 14 Jan 2006 01:02 GMT
> 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));
}
--
Mike W
VisionSet - 14 Jan 2006 01:04 GMT
> 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>();
--
Mike W
Roedy Green - 14 Jan 2006 03:47 GMT
>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.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 14 Jan 2006 03:49 GMT
>switch(myColor){
> case "blue":
[quoted text clipped - 4 lines]
> case "red":
> {
The code can look like this wit enums with methods.
setBackground( colourScheme.getColor() );

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
fb - 21 Jan 2006 02:07 GMT
> Hello, I have code that looks like this:
<SNIP Code>
Thanks for your help everyone...Got it working.