The java.util.Scanner class (added in Java 5) allows simple console and file input. Of course, your program should eventually have a GUI user interface, but Scanner is very useful for reading data files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// File : introductory/IntroScanner.java // Purpose: Write to and read from the console. // Author : Michael Maus // Date : 2006-01-20 import java.util.*; //Note 1 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. in.close(); //Note 2 //... Display output System.out.println("Take me to your leader, " + name); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// File : introductory/ScannerLoop.java
// Purpose: Read from the console in a loop using Scanner.
// Author : Michael Maus
// Date : 2006-01-20
import java.util.*;
public class ScannerLoop {
public static void main( |