/* TestScanner2.java CIS 160 David Klick 2015-09-02 Simple demonstration of Scanner class using the try-with-resources syntax to close a Scanner resource. */ import java.util.Scanner; public class TestScanner2 { public static void main(String[] args) { // Declare variables int n; double d; boolean b; String s; try (Scanner kbd = new Scanner(System.in)) { // Read an integer from the keyboard System.out.print("Enter an int: "); n = kbd.nextInt(); // Read a floating point number from the keyboard System.out.print("Enter a double: "); d = kbd.nextDouble(); // Read a Boolean value from the keyboard System.out.print("Enter a boolean: "); b = kbd.nextBoolean(); // Read a String from the keyboard System.out.print("Enter a String: "); kbd.nextLine(); // skip to next line s = kbd.nextLine(); // Display what was entered by the user System.out.println("You entered:\n " + n + "\n " + d + "\n " + b + "\n " + s); // Repeat display using printf System.out.printf("You entered:%n %d%n %f%n %b%n %s%n", n, d, b, s); // Note: The two output displays may not be exactly the same // because printf may show more precision for the floating // point number. } } }