import java.io.*; /** The Console class consists of static methods that allow easy reading of the basic data types double, int, and String from standard input. It must be placed in the same directory containing the source code using it.
Usage Example:
System.out.println("Enter a double number: ");
double x = Console.readDouble();
System.out.println("You have entered: " + x);
@author Bert G. Wachsmuth
@version 98/03/10
Modified on 9/9/01
by Marco T. Morazan
Changes: Added the readBoolean function.
Added print blank statements in each function to echo the input.
*/
public class Console
{
public static boolean readBoolean()
{ String tmpX = readString().trim();
try
{ return (new Boolean(tmpX)).booleanValue();
}
catch(NumberFormatException ne)
{ System.err.println("Not a boolean. Quitting ...");
System.exit(-1);
return false;
}
}
/** Reads a single double value from the keyboard. If there is an error, the
program will halt.
@return double
*/
public static double readDouble()
{ String tmpX = readString().trim();
try
{ return (new Double(tmpX)).doubleValue();
}
catch(NumberFormatException ne)
{ System.err.println("Not a double. Quitting ...");
System.exit(-1);
return 0.0;
}
}
/** Reads a single int value from the keyboard. If there is an error, the
program will halt.
@return int
*/
public static int readInt()
{ String tmpX = readString().trim();
try
{ return Integer.parseInt(tmpX);
}
catch(NumberFormatException ne)
{ System.err.println("Not an integer. Quitting ...");
System.exit(-1);
return -1;
}
}
/** Reads a String from the keyboard until RETURN or ENTER key is pressed.
@return String
*/
public static String readString()
{ String string = new String();
try
{ int cc = System.in.read();
while (! ((cc == 13) || (cc == 10)))
{ string += (char)cc;
cc = System.in.read();
}
long dummy = System.in.skip(2);
}
catch(IOException e)
{ }
System.out.println(string);
return string;
}
}