By Barry Burd

Java has all the operators that you need for mixing and matching logical tests. The operators are shown in this table. Check out each operator symbol, its meaning, and an example.

Logical Operators
Operator Symbol Meaning Example
&& and 5 < x && x < 10
|| or x < 5 || 10 < x
! not !password.equals(“swordfish”)

You can use these operators to form all kinds of elaborate conditions. This code listing has an example.

import javax.swing.JOptionPane;
public class Authenticator {
    public static void main(String args[]) {
        String username =
            JOptionPane.showInputDialog("Username:");
        String password = 
            JOptionPane.showInputDialog("Password:");
        if (
            username != null && 
            password != null && 
            (          
                (username.equals("bburd") &&
                 password.equals("swordfish")) ||
                (username.equals("hritter") &&
                 password.equals("preakston"))
            )
           )
        {
            JOptionPane.showMessageDialog
                (null, "You're in.");
        } else {
            JOptionPane.showMessageDialog
                (null, "You're suspicious.");
        }
    }
}

Several runs of the program of are shown in this figure. When the username is bburd and the password is swordfish or when the username is hritter and the password is preakston, the user gets a nice message. Otherwise, the user is gets the message shown.

Several runs of the code from the listing.

Several runs of the code from the listing.

The figure is a fake! To help you read the usernames and passwords, an extra statement was added to the listing. The extra statement (UIManager.put(“TextField.font”, new Font(“Dialog”, Font.BOLD, 14))) enlarges each text field’s font size.

The listing illustrates a new way to get user input; namely, to show the user an input dialog. The statement

String password = 
    JOptionPane.showInputDialog("Password:");

in the listing performs more or less the same task as the statement

String password = keyboard.next();

from the listing. The big difference is, while keyboard.next() displays dull-looking text in a console, JOptionPane.showInputDialog(“Username:”) displays a fancy dialog box containing a text field and buttons.

When the user clicks OK, the computer takes whatever text is in the text field and hands that text over to a variable. In fact, the listing uses JOptionPane.showInputDialog twice — once to get a value for the username variable, and a second time to get a value for the password variable.

Near the end of the listing, a slight variation on the JOptionPane business was used,

JOptionPane.showMessageDialog
    (null, "You're in.");

With showMessageDialog, a very simple dialog box is shown — a box with no text field. (Again, see the figure.)

Like thousands of other names, the name JOptionPane is defined in Java’s API. (To be more specific, JOptionPane is defined inside something called javax.swing, which in turn is defined inside Java’s API.) So to use the name JOptionPane throughout the listing, javax.swing.JOptionPane was imported at the top of the listing.

In the listing, JOptionPane.showInputDialog works nicely because the user’s input (username and password) are mere strings of characters. If you want the user to input a number (an int or a double, for example), you have to do some extra work.

For example, to get an int value from the user, type something like int numberOfCows = Integer.parseInt(JOptionPane.showInputDialog(“How many cows?”)). The extra Integer.parseInt stuff forces your text field’s input to be an int value. To get a double value from the user, type something like double fractionOfHolsteins = Double.parseDouble(JOptionPane.showInputDialog(“Holsteins:”)). The extra Double.parseDouble business forces your text field’s input to be a double value.