Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

Flavour Of The Month

And just as you can compare numbers, JSP also allows you to compare strings, with a couple of very useful String object methods.

First, the equals() method allows you to check whether the value of a particular string variable matches another. The following example should demonstrate this.

<%        
// define variables        
String myFavourite = "chocolate";        
String yourFavourite = "strawberry";        
       
// compare strings        
if (myFavourite.equals(yourFavourite))        
{        
out.println("A match made in heaven!");        
}        
else        
{        
out.println("Naw - try again!");        
}        
%>

Try changing the values of the variables to match each other, and gasp in awe as the output changes.

In case the equals() method doesn't appeal to you, JSP offers you a choice in the form of the compareTo() method, which returns a value indicating which of the two strings is greater. Take a look:

<%        
// define variables        
String alpha = "abcdef";        
String beta = "zyxwvu";        
       
// compare strings        
out.println(alpha.compareTo(beta));        
%>

In this case, if the value of the variable "beta" is greater than that of the variable "alpha", the compareTo() method will return a negative integer; if it's the other way around, the comparison will return a positive integer. And if the two strings are identical, the comparison will return 0.

Incidentally, the comparison is based on both the first character of the string, and the number of characters in the string. One string is considered "greater" than another if the numeric value of its first character is greater, or if its length is greater. In the example above, "z" has a greater numeric code than "a", and so the comparison will return a negative integer. But don't take our word for it -- try it yourself and see!

Turning Up The Heat

Why do you need to know all this? Well, comparison operators come in very useful when building conditional expressions -- and conditional expressions come in very useful when adding control routines to your code. Control routines check for the existence of certain conditions, and execute appropriate program code depending on what they find.

The first -- and simplest -- decision-making routine is the "if" statement, which looks like this:

if (condition)        
{        
do this!        
}

The "condition" here refers to a conditional expression, which evaluates to either true or false. For example,

if (hard drive crashes)        
{        
get down on knees and pray for redemption        
}

or, in JSP-lingo:

<%        
if (hdd == 0)        
{        
pray();        
}        
%>

If the conditional expression evaluates as true, all statements within the curly braces are executed. If the conditional expression evaluates as false, all statements within the curly braces will be ignored, and the lines of code following the "if" block will be executed.

Here's a simple program that illustrates the basics of the "if" statement.

<%!        
// declare temperature variable        
int temp = 50;        
%>        
       
<%        
// check temperature and display output        
if (temp > 30)        
{        
out.println("Man, it's hot out there!");        
}        
%>

In this case, a variable named "temp" has been defined, and initialized to the value 50. Next, an "if" statement has been used to check the value of the "temp" variable and display a message if it's over 30. Note our usage of the greater-than (>) conditional operator in the conditional expression.

An important point to note -- and one which many novice programmers fall foul of -- is the difference between the assignment operator [=] and the equality operator [==]. The former is used to assign a value to a variable, while the latter is used to test for equality in a conditional expression.

So:

a = 47;

assigns the value 47 to the variable a, while

a == 47

tests whether the value of a is equal to 47.
Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links