Using a switch statement with a String in JSP.

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
ramkumar_s1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
thanassisCommented:
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
TimYatesCommented:
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
TimYatesCommented:
Damn...sorry...

if( key != 0 )


should be

if( key != null )

too early in the morning ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.