Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Spring 2015
Class Notes—February 23, 2015
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes |

An Introduction to Interactive Program Construction

So far, we have written programs whose only form of interaction is through command-line arguments. This lab introduces you to the techniques you need for interaction with a user. Later in the term, we'll study how to build graphical user interfaces for our programs, but for now our focus is on reading input typed at the keyboard.

New Concepts

Scanner Methods:

Essential Techniques

Import the Scanner class

Unlike the other data types we've seen, the Scanner type is not visible by default. To use it in a program, you need to include the following line at the top of your program (before even the "public class" part):

import java.util.Scanner;

Set up a reader

Choose a name for the variable that will represent your reader. I'll use "inp" in my examples below, but there's no magic about this choice: any legal variable name will do. Before you try to use this reader for anything, you need to set it up by making a new one that is connected to System.in (no, this isn't done by default). This line should be at or near the beginning of your main() method:

Scanner inp = new Scanner(System.in);

Read input from the keyboard

Read a single word from the input:

String w = inp.next();

Read an entire line:

String ws = inp.nextLine();

Read an integer:

int x = inp.nextInt();

Other types (double, boolean), are handled similarly.

Example program:

import java.util.Scanner;    // always include this line

public class Greet {
  public static void main(String[] args) {
    Scanner inp = new Scanner(System.in);  // set up the reader
    
    System.out.print("Who are ye?  ");
    
    String name = inp.nextLine();  // read input, up to the ENTER key
    
    System.out.println("A tip o' the hat, " + name);
  }
}

Repeatedly read from the input:


while (inp.hasNextInt()) {
    int x = inp.nextInt();
}

Using Scanner instead of TextIO

Our textbook includes a number of specialized libraries to manage input and output. Among these is the TextIO library, for reading user-typed input. We're using Java's Scanner instead, as it's guaranteed to work with any Java system outside of HWS. The two are so close that it is fairly easy to learn from the textbook's discussion (beginning in section 2.4.3), with the substitution of a few details. On page 41 of your text is a table of the methods from the TextIO library, and the table below gives the corresponding methods for a Scanner.

Suppose we have set up our Scanner as above, storing it in the variable inp (or pick another, descriptive name):

TextIO Scanner
TextIO.getInt() inp.nextInt()
TextIO.getDouble() inp.nextDouble()
TextIO.getBoolean() inp.nextBoolean()
TextIO.getChar() no equivalent
TextIO.getWord() inp.next()
TextIO.getln() inp.nextLine()

Using System.out instead of TextIO

The text also includes output methods in the TextIO library. This has print() and println() methods, which behave exactly as their counterparts in System.out. In addition, there is a printf() method, for more controlled formatting (this also exists in System.out).

Some Exercises

  1. Modify any/all of the small programs you wrote in last Wednesday's class, so that they prompt for all of their arguments, rather than reading them from the command line.


John H. E. Lasseter