// Console.java 2003 Kari Laitinen // www.naturalprogramming.com // 2003-10-02 File created. // 2004-10-11 Last modification. // Comment added on Oct. 11, 2004: The new Java 1.5 version // provides a class named Scanner. With that class it is possible // to read the keyboard almost equally easily as with the // the class provided in this file. // This file defines a class named Console 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. // To find out how different methods are used, please // study Java programs provided by me. // 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. // If you use the Internet search engines, you can find // other Java classes that have the name Console and contain // static methods like readLine(), readInt(), readDouble(), // etc. Thus, this kind of class is almost a "de facto" // standard to read data from the keyboard. Probably the // first Console class has been provided in a book by // Cay Horstmann and Gary Cornell. The C# programming language, // "Microsoft's Java", has also a standard class named Console // to read data from the keyboard. // Before I found out that a class named Console // is commonly used in the Java world, I had developed my own // class named Keyboard to read data from the keyboard. // As I learned that Console is the "standard" class, I decided // to rename my Keyboard class to Console, and I also renamed // the methods so that they are the same as in other Console // classes. I believe that my Console class has more methods than // the other Console classes. The old Keyboard.java program is // available among my .java files if somebody is interested. import java.io.* ; class Console { public static String readLine() { String string_from_keyboard = new String() ; try { BufferedReader standard_input = new BufferedReader( new InputStreamReader( System.in ) ) ; string_from_keyboard = standard_input.readLine() ; } catch (IOException exception_not_processed ) { } return string_from_keyboard ; } // Method readString() works like readLine() public static String readString() { String string_from_keyboard = new String() ; try { BufferedReader standard_input = new BufferedReader( new InputStreamReader( System.in ) ) ; string_from_keyboard = standard_input.readLine() ; } catch (IOException exception_not_processed ) { } return string_from_keyboard ; } // Method trim() removes white space from // both ends of a string. public static byte readByte() { return Byte.parseByte( readLine().trim() ) ; } public static short readShort() { return Short.parseShort( readLine().trim() ) ; } public static int readInt() { return Integer.parseInt( readLine().trim() ) ; } public static int readIntBinary() { return Integer.parseInt( readLine().trim(), 2 ) ; } public static int readIntHexadecimal() { return Integer.parseInt( readLine().trim(), 16 ) ; } public static long readLong() { return Long.parseLong( readLine().trim() ) ; } public static char readChar() { String string_from_keyboard = readLine() ; return string_from_keyboard.charAt( 0 ) ; } public static float readFloat() { return Float.parseFloat( readLine().trim() ) ; } public static double readDouble() { return Double.parseDouble( readLine().trim() ) ; } public static void main( String[] not_used ) { System.out.print("\n\n Let's test class Console. Type something: " ) ; String given_string = readLine() ; System.out.print( "\n\n This I got: " + given_string ) ; /***************** System.out.print("\n\n Type in a short integer: " ) ; short given_short_integer = readShort() ; System.out.print( "\n\n This I got: " + given_short_integer ) ; System.out.print("\n\n Type in an int integer: " ) ; int given_integer = readInt() ; System.out.print( "\n\n This I got: " + given_integer ) ; ***********/ System.out.print("\n\n Type in a binary int integer: " ) ; int binary_integer = readIntBinary() ; System.out.print( "\n\n This I got: " + binary_integer ) ; System.out.print("\n\n Type in a hexadecimal int integer: " ) ; int hexadecimal_integer = readIntHexadecimal() ; System.out.print( "\n\n This I got: " + hexadecimal_integer ) ; /***************** System.out.print("\n\n Type in a double number: " ) ; double given_double_number = readDouble() ; 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 = readFloat() ; System.out.print( "\n\n This I got: " + given_float_number ) ; ****************/ } }