/* GetKeyboard2.java CIS 160 David Klick 2015-09-02 Get keyboard input - many items Note: This program will crash if the user enters unexpected input. */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class GetKeyboard2 { public static void main(String[] args) throws IOException { // Set up object to read from keyboard BufferedReader kbd = new BufferedReader( new InputStreamReader(System.in)); // Declare variables (one for each numeric data type) byte byteVar; short shortVar; int intVar; long longVar; float floatVar; double doubleVar; // Declare String so we can read input from keyboard String s; // Get input System.out.print("Enter a byte value (-128 to 127): "); s = kbd.readLine(); byteVar = Byte.parseByte(s); System.out.print("Enter a short integer (-32768 to 32767): "); s = kbd.readLine(); shortVar = Short.parseShort(s); System.out.print("Enter an integer: "); s = kbd.readLine(); intVar = Integer.parseInt(s); System.out.print("Enter a long integer: "); s = kbd.readLine(); longVar = Long.parseLong(s); System.out.print("Enter a floating-point number: "); s = kbd.readLine(); floatVar = Float.parseFloat(s); System.out.print("Enter a double (floating-point): "); s = kbd.readLine(); doubleVar = Double.parseDouble(s); // Display numbers entered System.out.println("The numbers entered were:"); System.out.println(" byte: " + byteVar); System.out.println(" short: " + shortVar); System.out.println(" int: " + intVar); System.out.println(" long: " + longVar); System.out.println(" float: " + floatVar); System.out.println("double: " + doubleVar); } } /* Sample of one run of program: Enter a byte value (-128 to 127): 120 Enter a short integer (-32768 to 32767): 12357 Enter an integer: 1569875 Enter a long integer: -4569875 Enter a floating-point number: 23.02 Enter a double (floating-point): 25.66987 The numbers entered were: byte: 120 short: 12357 int: 1569875 long: -4569875 float: 23.02 double: 25.66987 */