// Keyboard.java (c) 2003 Kari Laitinen // www.naturalprogramming.com // 2000-11-03 File created. // 2004-10-11 Last modification. // The methods provided in this file are not used in // the programs of my Java book. The Java version 1.5 // provides a class named Scanner with which it is possible // to read the keyboard in a rather convenient manner. // This file defines a class named Keyboard that contains // public static methods to read various data items from // the keyboard. This file is necessary because Java does // not provide simple standard methods to use the keyboard // in programs that are executed in the command prompt // window. // To use the methods of this class, you need to have // this file in the same directory (folder) as the // .java file that contains your program. // Note that at the beginning of your studies you do not // need to understand the inner structure of the methods // in this file. You just should learn how these methods // can be used in Java programs. import java.io.* ; class Keyboard { public static String get_string() { String string_from_keyboard = new String() ; try { BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ) ; string_from_keyboard = stdin.readLine() ; } catch (IOException ignored ) { } return string_from_keyboard ; } // Method trim() removes white space from // both ends of a string. public static byte get_byte() { return Byte.parseByte( get_string().trim() ) ; } public static short get_short() { return Short.parseShort( get_string().trim() ) ; } public static int get_int() { return Integer.parseInt( get_string().trim() ) ; } public static int get_int_binary() { return Integer.parseInt( get_string().trim(), 2 ) ; } public static int get_int_hexadecimal() { return Integer.parseInt( get_string().trim(), 16 ) ; } public static long get_long() { return Long.parseLong( get_string().trim() ) ; } public static char get_char() { String string_from_keyboard = get_string() ; return string_from_keyboard.charAt( 0 ) ; } public static float get_float() { return Float.parseFloat( get_string().trim() ) ; } public static double get_double() { return Double.parseDouble( get_string().trim() ) ; } public static void main( String[] command_line_parameters ) { System.out.print("\n\n Let's test class Keyboard. Type something: " ) ; String given_string = get_string() ; System.out.print( "\n\n This I got: " + given_string ) ; /******* System.out.print("\n\n Type in a short integer: " ) ; short given_short_integer = get_short() ; System.out.print( "\n\n This I got: " + given_short_integer ) ; System.out.print("\n\n Type in an int integer: " ) ; int given_integer = get_int() ; System.out.print( "\n\n This I got: " + given_integer ) ; *******/ System.out.print("\n\n Type in a binary int integer: " ) ; int binary_integer = get_int_binary() ; System.out.print( "\n\n This I got: " + binary_integer ) ; System.out.print("\n\n Type in a hexadecimal int integer: " ) ; int hexadecimal_integer = get_int_hexadecimal() ; System.out.print( "\n\n This I got: " + hexadecimal_integer ) ; /******* System.out.print("\n\n Type in a double number: " ) ; double given_double_number = get_double() ; System.out.print( "\n\n This I got: " + given_double_number ) ; System.out.print("\n\n Type in a float number: " ) ; float given_float_number = get_float() ; System.out.print( "\n\n This I got: " + given_float_number ) ; *******/ } }