?
Solved

Using a switch statement with a String in JSP.

Posted on 2003-03-04
4
Medium Priority
?
6,649 Views
Last Modified: 2006-11-17
I want to use a switch statement with a String in JSP. From the text books i read that a switch statement will support only int,boolean,char...but not string. Is there anyway that i could execute the code below.
 
String mOption = request.getParameter("Option");
switch(mOption)
  {
    case "DATE":
            if(mDate != pt1.getCollection_Date())
              mFlag = 1;
         break;
    case "FP":
            if(muser_id != pt1.getCollected_By())
              mFlag = 1;
         break;

    case "CUSTOMER":
            if(mCustomer != pt1.getCustomer_Id())
                mFlag = 1;
         break;
}

Regards,
Ramkumar
0
Comment
Question by:ramkumar_s1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 150 total points
ID: 8063318
nope...

Either:

String mOption = request.getParameter("Option");
if( mOption.equals( "DATE" ) )
{
           if(mDate != pt1.getCollection_Date())
             mFlag = 1;
}
else if( mOption.equals( "FP" ) )
{
           if(muser_id != pt1.getCollected_By())
             mFlag = 1;
}
else if( mOption.equals( "CUSTOMER" ) )
{
           if(mCustomer != pt1.getCustomer_Id())
               mFlag = 1;
}
0
 
LVL 4

Expert Comment

by:thanassis
ID: 8063323
Try this:

String mOption = request.getParameter("Option");
char temp = '';

if (mOption.equals("DATE"))
   temp = 'D';
else if (mOption.equals("FP"))
   temp = 'F';
else if (mOption.equals("CUSTOMER"))
   temp = 'C';

switch(temp)
 {
   case 'D':
           if(mDate != pt1.getCollection_Date())
             mFlag = 1;
        break;
   case 'F':
           if(muser_id != pt1.getCollected_By())
             mFlag = 1;
        break;

   case 'C':
           if(mCustomer != pt1.getCustomer_Id())
               mFlag = 1;
        break;
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8063326
or:

public static final int ID_DATE = 1 ;
public static final int ID_FP = 2 ;
public static final int ID_CUSTOMER = 3 ;

...

Hashtable keyHash = new Hashtable() ;
keyHash.put( "DATE", new Integer( ID_DATE ) ) ;
keyHash.put( "FP", new Integer( ID_FP ) ) ;
keyHash.put( "CUSTOMER", new Integer( ID_CUSTOMER ) ) ;

...

String mOption = request.getParameter("Option");
Integer key = keyHash.get( mOption ) ;
if( key != 0 )
{
switch( key.intValue() )
{
   case ID_DATE :
           if(mDate != pt1.getCollection_Date())
             mFlag = 1;
        break;
   case ID_FP :
           if(muser_id != pt1.getCollected_By())
             mFlag = 1;
        break;

   case ID_CUSTOMER :
           if(mCustomer != pt1.getCustomer_Id())
               mFlag = 1;
        break;
}
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8063340
Damn...sorry...

if( key != 0 )


should be

if( key != null )

too early in the morning ;-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question