slanted W3C logo

Day 13 — if

In today's lecture we look at the relational operators for primitive numeric types, validating user input, and Java's if statement.

Recap: boolean

A variable of type boolean can have only two values: true and false; they are used to store the result of a binary (yes/no, true/false, on/off) decision.

Relational Operators

For the primitive numeric types (int, double, etc.) the relational operators can be used to test values for equality and inequality.

Expression Meaning Value
8 < 15 8 is less than 15 true
8 > 15 8 is greater than 15 false
200L <= 300L 200 is less than or equal to 300 true
100.1 >= 100 100.1 is greater than or equal to 100 true
3 == 3 3 is equal to 3 true
1 != 2 1 is not equal to 2 true

Validating User Input

So far the problems we have been studying have ignored the issue of checking that the values entered by the user are sensible.

Failure to validate user input is a common cause of computer security breaches:

Validating User Input Using type.lib.ToolBox

Suppose you want to make sure that some user supplied input value is greater than or equal to zero (the value might represent an amount of money, distance, mass, a quantity, etc.).

import type.lib.ToolBox;
import java.io.PrintStream;
import java.util.Scanner;

public class PositiveInput1
{
   public static void main(String[] args)
   {
      PrintStream output = System.out;
      Scanner input = new Scanner(System.in);

      output.print("Enter the value for ... : ");
      double userValue = input.nextDouble();
      ToolBox.crash(userValue < 0.0, "Input cannot be negative");

      // the rest of your code here
   }
}

If the user supplies a negative value to the program above, it will stop execution (at the line in red) and output Input cannot be negative (along with some other information).

What Is ToolBox.crash Doing?

The previous example basically follows these steps:

1. ask the user for a value
2. get the value
3. if the value is negative
      display an error message
      stop execution
4. ...

You can express Step 3 in Java using an if statement:

      output.print("Enter the value for ... : ");
      double userValue = input.nextDouble();
      if (userValue < 0.0)
      {
         output.println("Input cannot be negative");
         System.exit(1);
      }

      // the rest of your code here

Editing User Input

Instead of stopping the program, you might consider "fixing" the user input:

1. ask the user for a value
2. get the value
3. if the value is negative
      set the value to zero
4. ...
      output.print("Enter the value for ... : ");
      double userValue = input.nextDouble();
      if (userValue < 0.0)
      {
         userValue = 0.0;
      }

      // the rest of your code here

if Statement

Java's if statement lets you choose to execute a block of code depending on a logical expression (an expression that evaluates to type boolean).


      if (logical-expression)
      {
         statement(s)
      }

Example

Suppose you work as a ride attendant at an amusement park. Your ride has a capacity of 32 people. Write a Java program that asks the user for the number of people in line waiting to get on the ride, and outputs the number of people who can safely get on the ride.

1. ask the user for a value
2. get the value
3. if the value is greater than 32
      set the value to 32
4. output the value
      final int CAPACITY = 32;
      output.print("Enter the number of people in line : ");
      int numInLine = input.nextInt();
      if (numInLine > CAPACITY)
      {
         numInLine = CAPACITY;
         output.println("Busy day!");
      }
      output.printf("%d people can get on the ride%n", numInLine);

if Statement Braces

The braces are important! If you forget them, Java will use only the first statement after the if (logical-expression) for the statement(s) block. For example, this fragment:

      int numInLine = input.nextInt();
      if (numInLine > CAPACITY)
         numInLine = CAPACITY;
         output.println("Busy day!");
      output.printf("%d people can get on the ride%n", numInLine);

is equivalent to this fragment:

      int numInLine = input.nextInt();
      if (numInLine > CAPACITY)
      {
         numInLine = CAPACITY;
      }
      output.println("Busy day!");
      output.printf("%d people can get on the ride%n", numInLine);

In-Class Exercise

Get the handout here.

To Do For Next Lecture

  1. Read Section 5.2 of the textbook.