Java 5's java.util.Scanner class has simplified console I0. Compare this example to how it's written before Java 5 (see Console Input-Output (pre Java 5))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// File: introductory/IntroScanner.java // Purpose: Write to and read (using Scanner) from the console. // Author: Michael Maus // Date: 2005-01-20 import java.util.*; public class IntroScanner { public static void main(String[] args) { String name; // Declare a variable to hold the name. Scanner in = new Scanner(System.in); System.out.println("What's your name, Earthling?"); name = in.nextLine(); // Read one line from the console. System.out.println("Take me to your leader, " + name); System.exit(0); // Not necessary, but commonly used. } } |