Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Switch on Strings

Thread view: 
fb - 14 Jan 2006 00:32 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.