import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * The purpose of this class is to provide convenient methods for reading input * from the keyboard. *
* Two sets of read methods are provided. *
*
readInt(), readDouble(),
 * readChar(), readBoolean(),
 * readString(): read one value of the specified type
 * readIntLine(),readDoubleLine(),
 * readCharLine(): read all values from a line, create an array of the
 * proper length, and put the values into the array.
 * * None of these methods allow values of different types to be read from the * same line of input. *
 * Note that Integer.parseInt() does not allow a leading + so,
 * for example, +123 will be rejected. However, the double reader
 * does not require a leading 0 so, for example, -.2 will be accepted.
 * 
 * 
 * @author Alan Williams
 */
public class ITI1120
{
	private static final BufferedReader keyboard = new BufferedReader(
			new InputStreamReader( System.in ) );
	/**
	 * The private constructor means that instances of this class cannot be
	 * constructed.
	 */
	private ITI1120( )
	{
	}
	/**
	 * Read a single int value from the keyboard.
	 * 
	 * If the user enters a non-int value, a
	 * {@link NumberFormatException} will be thrown, and the program will
	 * terminate. Typing return without entering any value will also produce
	 * this result.
	 * 
	 * @return An int containing the value entered from the
	 *         keyboard.
	 */
	public static int readInt( )
	{
		// Get the user's input string.
		String inputString = getInput( );
		return Integer.parseInt( inputString );
	}
	/**
	 * Read a single double value from the keyboard.
	 * 
	 * If the user enters a non-double value, a
	 * {@link NumberFormatException}will be thrown, and the program will
	 * terminate. Typing return without entering any value will also produce
	 * this result.
	 * 
	 * @return A double containing the value entered from the
	 *         keyboard.
	 */
	public static double readDouble( )
	{
		// Get the user's input string.
		String inputString = getInput( );
		return Double.parseDouble( inputString );
	}
	/**
	 * Read a single boolean value from the keyboard.
	 * 
	 * If the user enters any variation of the word "true" in upper, lower, or
	 * mixed case, the result will be the boolean value
	 * true. Otherwise, the result will be the
	 * boolean value false.
	 * 
	 * @return A boolean containing the value entered from the
	 *         keyboard.
	 */
	public static boolean readBoolean( )
	{
		// Get the user's input string.
		String inputString = getInput( );
		// The following will convert the String to a boolean. Any version of
		// the word "true" should result in a true value; all other strings
		// will result in a value of false.
		return Boolean.valueOf( inputString ).booleanValue( );
	}
	/**
	 * Read a single char value from the keyboard.
	 * 
	 * If the types return without entering any character, a
	 * {@link StringIndexOutOfBoundsException} will be thrown, and the program
	 * will terminate. Otherwise, the first character of what the user typed
	 * will be returned as a char value.
	 * 
	 * @return A char containing the value entered from the
	 *         keyboard.
	 */
	public static char readChar( )
	{
		// Get the user's input string.
		String inputString = getInput( );
		// Return first character from string.
		return inputString.charAt( 0 );
	}
	/**
	 * Read an array of double values from the keyboard.
	 * 
* The values must be separated by at least one space as they are entered. *
	 * If the user enters a non-double value, a
	 * {@link NumberFormatException}will be thrown, and the program will
	 * terminate.
	 * 
	 * If all values are legal double values, a new array is
	 * created. The length of the array is set by counting the number of values
	 * entered by the user. The values are then entered into the corresponding
	 * array positions.
	 * 
	 * @return An array of double values containing the
	 *         set of values entered from the keyboard.
	 */
	public static double[] readDoubleLine( )
	{
		// Get the user's input string.
		String inputString = getInput( );
		// The StringTokenizer will split the string at each run of
		// white space characters.
		StringTokenizer st = new StringTokenizer( inputString );
		// Find out how many values were entered.
		int numberOfValues = st.countTokens( );
		// Create the array.
		double[] values = new double[numberOfValues];
		// Each token will be a String. Try to convert each token from
		// a String to a double. If any value doesn't convert, the
		// exception will be thrown.
		for ( int index = 0; index < numberOfValues; index = index + 1 )
		{
			// Get the next token.
			String stringValue = st.nextToken( );
			// Convert token to a double, and insert in array.
			values[index] = Double.parseDouble( stringValue );
		}
		return values;
	}
	/**
	 * Read an array of int values from the keyboard.
	 * 
* The values must be separated by at least one space as they are entered. *
	 * If the user enters a non-int value, a
	 * {@link NumberFormatException} will be thrown, and the program will
	 * terminate.
	 * 
	 * If all values are legal double values, a new array is
	 * created. The length of the array is set by counting the number of values
	 * entered by the user. The values are then entered into the corresponding
	 * array positions.
	 * 
	 * @return An array of double values containing the
	 *         set of values entered from the keyboard.
	 */
	public static int[] readIntLine( )
	{
		// Get the user's input string.
		String inputString = getInput( );
		// The StringTokenizer will split the string at each run of
		// white space characters.
		StringTokenizer st = new StringTokenizer( inputString );
		// Find out how many values were entered.
		int numberOfValues = st.countTokens( );
		// Create the array.
		int[] values = new int[numberOfValues];
		// Each token will be a String. Try to convert each token from
		// a String to an int. If any value doesn't convert, the
		// exception will be thrown.
		for ( int index = 0; index < numberOfValues; index = index + 1 )
		{
			// Get the next token.
			String stringValue = st.nextToken( );
			// Convert token to an int, and insert in array.
			values[index] = Integer.parseInt( stringValue );
		}
		return values;
	}
	/**
	 * Read an array of char values from the keyboard.
	 * 
* After the user has entered a string of characters, hitting 'enter' * returns to the calling method. All characters including spaces are put * in the array. *
	 * The String entered by the user is converted to an array of characters of
	 * the appropriate length.
	 * 
	 * @return An array  of char values containing the set
	 *         of characters entered from the keyboard.
	 */
	public static char[] readCharLine( )
	{
		// Get the user's input string.
		String inputString = getInput( );
		// Convert the entire String that was entered to an array
		// of chars.
		return inputString.toCharArray( );
	}
	/**
	 * Read a String from the keyboard.
	 * 
* * @return A {@link String} containing the user's input. */ public static String readString( ) { // Get the user's input string. String inputString = getInput( ); // Return the string that was entered. return inputString; } /** * This method gets the input from the keyboard as a String. *
	 * The intention of this method is to catch the possible {@link IOException}
	 * that could be thrown by the readline() method, so that the
	 * main() method does not have to declare that it throws this
	 * exception.
	 * 
* If the exception occurs, a stack trace is printed and the program is * terminated. * * @return A {@link String} with the input typed from the keyboard. */ private static String getInput( ) { String inputString = null; try { inputString = keyboard.readLine( ); } catch ( IOException e ) { e.printStackTrace( ); // The following may not work real well with Dr. Java System.exit( 0 ); } return inputString; } }