 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
fb Guest
|
Posted: Sat Jan 14, 2006 12:32 am Post subject: 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
|
|
| Back to top |
|
 |
Andrew McDonagh Guest
|
Posted: Sat Jan 14, 2006 12:54 am Post subject: Re: Switch on Strings |
|
|
fb wrote:
| Quote: | Hello, I have code that looks like this:
|
snipped switch statement
| Quote: |
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.
|
|
| Back to top |
|
 |
VisionSet Guest
|
Posted: Sat Jan 14, 2006 1:02 am Post subject: Re: Switch on Strings |
|
|
"fb" <fb (AT) noway (DOT) com> wrote
| Quote: |
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
|
|
| Back to top |
|
 |
VisionSet Guest
|
Posted: Sat Jan 14, 2006 1:04 am Post subject: Re: Switch on Strings |
|
|
"VisionSet" <spam (AT) ntlworld (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sat Jan 14, 2006 3:47 am Post subject: Re: Switch on Strings |
|
|
On Sat, 14 Jan 2006 00:32:18 GMT, fb <fb (AT) noway (DOT) com> wrote, quoted or
indirectly quoted someone who said :
| Quote: |
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.
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sat Jan 14, 2006 3:49 am Post subject: Re: Switch on Strings |
|
|
On Sat, 14 Jan 2006 00:32:18 GMT, fb <fb (AT) noway (DOT) com> wrote, quoted or
indirectly quoted someone who said :
| Quote: | 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.
|
|
| Back to top |
|
 |
fb Guest
|
Posted: Sat Jan 21, 2006 2:07 am Post subject: Re: Switch on Strings |
|
|
fb wrote:
| Quote: | Hello, I have code that looks like this:
SNIP Code |
Thanks for your help everyone...Got it working.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|