Re: How do I make a string comparison switch?

From: Chris Smith (cdsmith_at_twu.net)
Date: 05/30/04


Date: Sun, 30 May 2004 15:42:05 -0600

Panama Red wrote:
> I want to match a string against a series of different
> keywords... here is what Ive tried :
>
> switch(Request) {
>
> case Request.equals("HELLO"):
> // Write Results to Monitor
> logDisplay.append( "Results: HELLO BACK\n");
> break;
> }
>
> The compiler tells me I need an int, not a string ... so how do
> you do this common task?

if (Request.equals("HELLO"))
{
    // Write Results to Monitor
    logDisplay.append("Results: HELLO BACK\n");
}

If there are more possibilities, then:

else if (Request.equals("GOODBYE"))
{
    ...
}
else if (Request.equals("THANKYOU"))
{
    ...
}

and so forth.

Incidentally, it could trip up people who are trying to help you if you
use bad variable names in posts to public newsgroups. The practically-
universal Java naming convention is to begin variable names with a
lower-case letter (for example, request instead of Request) -- unless
they are constants, in which case you'd use all-caps.

-- 
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Relevant Pages

  • Re: c# interview question
    ... particular version of the compiler happens to generate. ... internally will call Concat(string, string). ... shows the second loop runs 5-10pc quicker than the first loop. ... int s1 = System.Environment.TickCount; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Broken STL implementation?
    ... > int main ... map operations will call operator< which is supposed to ... bit about g++ handles string constants. ...
    (comp.lang.cpp)
  • Re: Controlling Javascript from server side
    ... but five different language implementations here. ... 'true' means that the request must be handled asynchronously. ... There is exactly *no* reason for such a thing here. ... | percent-endoded string). ...
    (comp.lang.javascript)
  • Re: Execute binary code
    ... If the executable code is stored in string, ... address to a function pointer (possibly through a void* if the compiler ... then dereference the function pointer. ... int arg1, arg2, arg3; ...
    (comp.lang.python)
  • Re: Is it ok to change $ENV{QUERY_STRING} before "use CGI;" is called..?
    ... the big advantage of wiki, ... It does url-encode if its option is set to use UTF-8 request, ... string for GET requests. ... because "query string" in URL is anyway just a string ...
    (comp.lang.perl.misc)