Comparing Values
For your program to make decisions, it must compare values. In this chapter I will show you how to compare different types of values.
Comparing a Numeric Value
First I will show you how to compare a variable to a numeric value. This is done by simply using ==. For example, if you wanted to compare the variable “i”, you could use the following lines of code.
int i=5;
if( i==5 )
{
System.out.println("i contains the value 5");
}
Very important! Notice how I used the == to compare the value of “i”. This is how you compare values in Java. A single = means assign the value, such as:
i=5;
The above statement will assign the value of 5 to i. Do not confuse the assignment (=) with the comparison (==).
Now lets look at a complete example. Listing 5.1 shows a program that compares to see if a variable is above a certain number or not.
Listing 5.1: Are You Old Enough (OldEnough.java)
import java.io.*;
class OldEnough
{
public static void main(String args[])
{
try
{
InputStreamReader inputStreamReader =
new InputStreamReader ( System.in );
BufferedReader in =
new BufferedReader ( inputStreamReader );
System.out.print("How old are you? ");
String age = in.readLine();
int iAge = Integer.parseInt(age);
if( iAge>=18 )
{
System.out.println(
"You are old enough to vote in the United States.");
}
if( iAge<18 )
{
System.out.println(
"You are not old enough to vote in" +
" the United States");
}
}
catch(NumberFormatException e)
{
System.out.println("That is not a valid age.");
}
catch(IOException e)
{
}
}
}This program will prompt the user to see how old they are. If the user is 18 or over, the user will be informed that they are old enough to vote in the United States. If the user is below 18 years, the user will be informed that they are not old enough to vote in the United States.
This program does this by using the “if statement” to check the age of the user.
if( iAge>=18 )
{
System.out.println(
"You are old enough to vote in the United States.");
}
Notice the if statement. It names the variable to compare and what to compare it against. Here we are checking to see if the variable “iAge” is greater than, or equal to, the value 18.
Comparing a String Value
Comparing a string is somewhat different than comparing a number. You may be tempted to use == to compare a string. This will not work properly, even though it will compile correctly. Consider Listing 5.2.
Listing 5.2: Compare a String (InvalidString.java)
import java.io.*;
class InvalidString
{
public static void main(String args[])
{
try
{
InputStreamReader inputStreamReader =
new InputStreamReader ( System.in );
BufferedReader in =
new BufferedReader ( inputStreamReader );
System.out.print(
"What is your favorite color? ");
String color = in.readLine();
if( color=="red" )
System.out.println(
"My favorite color is red too!");
}
catch(IOException e)
{
}
}
}This example program will not work. It does not properly compare two strings. This is a very confusing aspect of Java, but unfortunately you cannot use the double equal (==) to compare two strings. Using == asks Java if the two strings occupy the same location in memory. Normally you really don’t care if two strings are at the same location in memory, so using == with strings is unsuitable.
To properly compare two strings you must use the “equals” method of the String class. Listing 5.3 shows the same program that we just examined, only using the “equals” method.
Listing 5.3: Check for a Valid String (ValidString.java)
import java.io.*;
class ValidString
{
public static void main(String args[])
{
try
{
InputStreamReader inputStreamReader =
new InputStreamReader ( System.in );
BufferedReader in =
new BufferedReader ( inputStreamReader );
System.out.print(
"What is your favorite color? ");
String color = in.readLine();
if( color.equals("red") )
System.out.println(
"My favorite color is red too!");
}
catch(IOException e)
{
}
}
}












