Java: Console Input-Output (Java 5)

Java 5's java.util.Scanner class has simplified console I0.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
// File   : introductory/IntroScanner.java
// Purpose: Write to and read from the console.
// Author : Michael Maus
// Date   : 2005-03-29

import java.util.*;

public class IntroScanner {

    public static void main(String[] args) {
        //... Initialization
        String name;                // Declare a variable to hold the name.
        Scanner in = new Scanner(System.in);

        //... Prompt and read input.
        System.out.println("What's your name, Earthling?");
        name = in.nextLine();      // Read one line from the console.

        //... Display output
        System.out.println("Take me to your leader, " + name);
    }
}