Question using if/else statement in JSP

M

Mike

I have a jsp that is taking data entered into an html form and posting
it to an MS-SQL DB. This is working fine.

Before I post the data I want the jsp to calculate a shipping charge
based on where the person is located. Here is some of my code.
START CODE
String t_state = request.getParameter("state");
String t_postal = request.getParameter("zip");
String t_country = request.getParameter("country");
.......
.......
// Calculate the shipping costs
int shp_cost=0;
if(t_country=="US") {
if(t_state=="AK"||t_state=="HI"||t_state=="PR") {
shp_cost=15;
} else {shp_cost=10;
};
} else if(t_country=="CA"||t_country=="MX") {
shp_cost=15;
} else {
shp_cost=25;
}
END CODE

Right after this I post the t_country, t_state and shp_cost to my db
and that posts everything correctly.

My problem is that no matter what I enter into the form for these 2
strings, the shp_cost always comes out =25.
I don't believe that it is the IF/Else statement because if I add
lines like
t_country="US";
t_state="ID";
the If/Else statement works fine and would return in this case
shp_cost=10

So I am thinking that I need to do something else with the 2 strings,
but not sure what. Like I said they post fine to the db, so the data
is assigned to the strings.

Any ideas?
Thanks in advance for the help.
 
C

Chris Smith

Mike said:
I have a jsp that is taking data entered into an html form and posting
it to an MS-SQL DB. This is working fine.

The problem is that you need to learn Java before you learn JSP. String
comparison in Java looks like this:

if (str.equals("Something")) ...;

and not like this:

if (str == "Something") ...;

The latter checks for identity of objects, and is not appropriate for
comparing value objects such as java.lang.String.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Andoni

t_country is a String object. It is never going to be == any of the other
String objects in your JSP no matter what values those objects have.

If you want to compare one String object to another in Java to see if they
both contain the same string you have to use the equals() method of the
string object thus:

START CODE
String t_state = request.getParameter("state");
String t_postal = request.getParameter("zip");
String t_country = request.getParameter("country");
.......
.......
// Calculate the shipping costs
int shp_cost=0;
if(t_country.equals("US")) {
if(t_state.equals("AK")||t_state.equals("HI")||t_state.equals("PR")) {
shp_cost=15;
} else {
shp_cost=10;
};
} else if(t_country.equals("CA")||t_country.equals("MX")) {
shp_cost=15;
} else {
shp_cost=25;
}
END CODE


Hope that helps,

Andoni.
 
M

Mike

Andoni said:
t_country is a String object. It is never going to be == any of the other
String objects in your JSP no matter what values those objects have.

If you want to compare one String object to another in Java to see if they
both contain the same string you have to use the equals() method of the
string object thus:

START CODE
String t_state = request.getParameter("state");
String t_postal = request.getParameter("zip");
String t_country = request.getParameter("country");
......
......
// Calculate the shipping costs
int shp_cost=0;
if(t_country.equals("US")) {
if(t_state.equals("AK")||t_state.equals("HI")||t_state.equals("PR")) {
shp_cost=15;
} else {
shp_cost=10;
};
} else if(t_country.equals("CA")||t_country.equals("MX")) {
shp_cost=15;
} else {
shp_cost=25;
}
END CODE


Hope that helps,

Andoni.

Thanks, I'll give that a try.
And yes whoever said that I need to learn Java is correct, I am just
learning. But what better way then to jump into it. That is all that
this is is learning, and really my only interest right now is form &
db integration so this little project is something that makes sense to
me and excellerates my learning curve.

Thanks.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top