/** * ========================================================================== * Scanner1.java: The class Scanner provides support for parsing of primitive * data types (e.g., int, float and double) and strings. A Scanner breaks its * input into tokens, which by default will be delimited by whitespace. The * tokens may then be converted into values of different types using the * various next methods. * * This program demonstrate use of a Scanner for input of text, integers, * and floating-point numbers from the keyboard (i.e., System.in). * * Written by: Mark Austin June 2009 * ========================================================================== */ import java.util.Scanner; import java.util.InputMismatchException; public class Scanner1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); try { // Prompt the user for his/her first name. System.out.print("Please enter your FIRST name: "); String firstName = sc.next(); System.out.print("Please enter your FAMILY name: "); String familyName = sc.next(); // Gather data ..... System.out.print("Please enter your age as an integer: "); int iAge = sc.nextInt(); System.out.print("Please enter your GPA as a double: "); double dGPA = sc.nextDouble(); // Print summary of input ..... System.out.println(); System.out.printf("%s %s is %d years old\n", firstName, familyName, iAge ); System.out.printf("%s\'s GPA is %f\n", firstName, dGPA ); } catch (InputMismatchException e) { System.out.println(); System.out.println("Error: You didn't follow the instructions " + "properly; please try again."); } } }