import java.util.Scanner;

// Simple program using a Scanner to read from the keyboard in Java

public class SimpleScanner {

    public static void main(String[] args) {

	// Scanner Declaration 
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter a line of text: ");
	// Reading a full line
        String line = keyboard.nextLine();
        System.out.println("You entered: " + line);

        System.out.print("Enter a single token (word): ");
	// Reading one word
        String token = keyboard.next();
        System.out.println("You entered: " + token);

        System.out.print("Enter a double: ");
	// Reading a double 
        double d = keyboard.nextDouble();
        System.out.printf("You entered %.5f\n", d);
        
	System.out.print("Enter an integer: ");
	// Reading an integer
        int i = keyboard.nextInt();
        System.out.printf("You entered: %d\n", i);

	// Closing a Scanner
	keyboard.close();
    }
}

© 2012 CS160 Colorado State University. All Rights Reserved.