JMU
Console Input and Output in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Understanding Streams
"Standard" Streams
"Standard" Streams in Java
A Brief Quiz
Encapsulation of Standard Streams
Using a PrintStream Object
Using a PrintStream Object (cont.)
The Format String
Using a PrintStream Object (cont.)

An Example that uses printf()

javaexamples/iobasics/PrintfExample.java
import java.io.PrintStream;

/**
 * An example that illustrates the use of the printf()
 * method in the PrintStream class
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class PrintfExample
{
    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
       int     age, weight;
       double  monday, tuesday, wednesday;
       
       age = 27;
       
       System.out.printf("%d\n", age);
       System.out.printf("%10d\n", age);
       
       weight = 155;
       
       System.out.printf("Prof. B. is %d years old\n", age);
       System.out.printf("Age: %d\tWeight: %d\n", age, weight);
       

       monday    =  35.8;
       tuesday   =   0.0;
       wednesday = -10.2;

       System.out.println("\nWith No Flags, Width or Precision");       
       System.out.printf("%f\n", monday);
       System.out.printf("%f\n", tuesday);
       System.out.printf("%f\n", wednesday);

       System.out.println("\nWith No Flags");       
       System.out.printf("%5.1f\n", monday);
       System.out.printf("%5.1f\n", tuesday);
       System.out.printf("%5.1f\n", wednesday);

       System.out.println("\nWith + Flags");       
       System.out.printf("%+5.1f\n", monday);
       System.out.printf("%+5.1f\n", tuesday);
       System.out.printf("%+5.1f\n", wednesday);
    }
}
        
Using an InputStream Object
Using a Scanner Object
Using a Scanner Object (cont.)

An Example that uses a Scanner

javaexamples/iobasics/ScannerExample.java
import java.util.Scanner;

/**
 * An example that illustrates the use of the Scanner class
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ScannerExample
{
    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
       int        age, weight;
       double     salary;
       Scanner    keyboard;
       String     line, name;
       String[]   parts;       
       
       keyboard = new Scanner(System.in);
       
       System.out.print("Name: ");
       name = keyboard.nextLine();

       System.out.print("Salary: ");
       salary = keyboard.nextDouble();
       keyboard.nextLine(); // Consume the end-of-line character

       System.out.print("Phone number (###-###-####): ");
       line   = keyboard.nextLine();

       System.out.print("Age Weight (# #): ");
       age    = keyboard.nextInt();
       weight = keyboard.nextInt();

       keyboard.close();       
    }
}
        
Processing Input with a Scanner (cont.)

An Inconvenience when Using Scanner Objects

javaexamples/basics/ScannerInconvenienceExample.java
import java.util.Scanner;

/**
 * An example that illustrates an inconvenience assoicated with
 * the behavior of Scanner objects.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ScannerInconvenienceExample
{
    /**
     * The entry point of the application.
     *
     * @param args The command-line arguments (which are ignored)
     */
    public static void main(String[] args)
    {
        int     age;
        Scanner keyboard;        
        String  name;
        
        keyboard = new Scanner(System.in);
        
        // If you run this program you won't be able to enter your
        // name because the return you enter after your age will
        // be considered the "next line"
        System.out.print("Enter your age: ");
        age = keyboard.nextInt();
        System.out.print("Enter your name: ");
        name = keyboard.nextLine();

        keyboard.close();
    }
}

        
Using a Scanner Object (cont.)
The "Modern" Approach to Console I/O