switch case statement

V

vic

Hi All,

I have a question regarding structuring my servlet. I have a couple of
if conditions like this

if((String)request.getParameter("test").equals("a")) {do something
.........}
if((String)request.getParameter("test").equals("b")) {do something
.........}
if((String)request.getParameter("test").equals("c")) {do something
.........}
if((String)request.getParameter("test").equals("d")) {do something
.........}
if((String)request.getParameter("test").equals("e")) {do something
.........}

how do i go about structuring these if conditions using a switch case
statement

i am looking for something like this, how do i pass the choice and
evaluate the case block .

switch(choice)

case 1: { }

case 2: { }

case 3: { }
..
..
..
..
..

Any help appreciated
 
R

Ryan Stewart

vic said:
Hi All,

I have a question regarding structuring my servlet. I have a couple of
if conditions like this

if((String)request.getParameter("test").equals("a")) {do something
........}
if((String)request.getParameter("test").equals("b")) {do something
........}
if((String)request.getParameter("test").equals("c")) {do something
........}
if((String)request.getParameter("test").equals("d")) {do something
........}
if((String)request.getParameter("test").equals("e")) {do something
........}

how do i go about structuring these if conditions using a switch case
statement

i am looking for something like this, how do i pass the choice and
evaluate the case block .

switch(choice)

case 1: { }

case 2: { }

case 3: { }
.
.
.
.
.

Any help appreciated

What you have above can't be converted directly to a switch statement
because switch only takes ints (which means you can give it an int, short,
byte, or char). So you can't give it a String. However, if your parameter
will always be a single character ('a', 'b', 'c', '1', ']', etc), you can
first convert it to a char, then use a switch statement:
char param = ((String) request.getParameter("test")).charAt(0);
switch (param) {
case 'a':
// Something
break;

case 'b':
// Something else
break;

default:
// The stuff that happens if nothing else does
break;
}
 
B

Bill French

vic,

hello. java requires break statements for every case. so a switch
would look like this (copied from o'reilly's java in a nutshell):

switch(n) {
case 1:
// Execute code block #1
break;
case 2:
// Execute code block #2
break;
case 3:
// Execute code block #3
break;
default:
// if all else fails, execute
// code block #4
break;
}
 
T

Tony Morris

hello. java requires break statements for every case. so a switch
would look like this (copied from o'reilly's java in a nutshell):

No, it doesn't
Most application logic usually turns out such that a break statement is
required.
There is no other requirement besides that which is application dependant.

A better design would use a java.util.Map and/or polymorphism to achieve
such a thing.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
J

Johnson Bruno

Store the result of (String)request.getParameter("test").equals("a")
in a String variable say result and use result.charAt(0) in switch

The following code will give you an idea,I believe :)

String result =
(String)request.getParameter("test").equals("a") ;

switch(result.charAt(0)) {
case 'a' : System.out.println("a"); break;
case 'b' : System.out.println("b"); break;
case 'c' : System.out.println("c"); break;
}
 
T

Tony Morris

String result =
(String)request.getParameter("test").equals("a") ;

That cast is redundant.
If you meant:
String result = (String)request.getAttribute("test").equals("a") ;

(for whatever reason), it won't work anyway.

String result = ((String)request.getAttribute("test")).equals("a") ;
or
String result = request.getParameter("test").equals("a") ;

Either way, it's poor form.
There are many MVC frameworks out there that do this sort of this in a much
nicer fashion.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
W

William Brogden

vic said:
Hi All,

I have a question regarding structuring my servlet. I have a couple of
if conditions like this

if((String)request.getParameter("test").equals("a")) {do something
........}
if((String)request.getParameter("test").equals("b")) {do something
........}
if((String)request.getParameter("test").equals("c")) {do something
........}
if((String)request.getParameter("test").equals("d")) {do something
........}
if((String)request.getParameter("test").equals("e")) {do something
........}

how do i go about structuring these if conditions using a switch case
statement

i am looking for something like this, how do i pass the choice and
evaluate the case block .

I like to do that with a Hashmap where the word is the key and
the retrieved value is an Integer.
 
T

Thomas Weidenfeller

William said:
I like to do that with a Hashmap where the word is the key and
the retrieved value is an Integer.

I would suggest a small change:

Let the retrieved value be a reference to a "handler object". An object
which has methods to handle that specific case.

Object o = theMap.get(parameter);
// check for valid object (includes an implicit check for null)
if(o instanceof HandlingInterface) {
((HandlingInterface)o).doIt(...);
}



/Thomas
 
W

William Brogden

Thomas Weidenfeller said:
I would suggest a small change:

Let the retrieved value be a reference to a "handler object". An object
which has methods to handle that specific case.

Object o = theMap.get(parameter);
// check for valid object (includes an implicit check for null)
if(o instanceof HandlingInterface) {
((HandlingInterface)o).doIt(...);
}



/Thomas

Although that sounds cool and "computer sciencey", in practice it is only
useful if the actions are very similar. In most cases you would end up
writing lots of silly classes.
Bill
 
V

vic

Tony Morris said:
That cast is redundant.
If you meant:
String result = (String)request.getAttribute("test").equals("a") ;

(for whatever reason), it won't work anyway.

String result = ((String)request.getAttribute("test")).equals("a") ;
or
String result = request.getParameter("test").equals("a") ;

Either way, it's poor form.
There are many MVC frameworks out there that do this sort of this in a much
nicer fashion.


Thanks a bunch guys. Aprreciate your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top