Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7002
  • Last Modified:

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
0
ramkumar_s1
Asked:
ramkumar_s1
  • 3
1 Solution
 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now