All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class utils.SavitchIn

java.lang.Object
   |
   +----utils.SavitchIn

public class SavitchIn
extends Object
Class for simple console input. A class designed primarily for simple keyboard input of the form one input value per line. If the user enters an improper input, i.e., an input of the wrong type or a blank line, then the user is prompted to reenter the input and given a brief explanation of what is required. Also includes some additional methods to input single numbers, words, and characters, without going to the next line.


Constructor Index

 o SavitchIn()

Method Index

 o read()
Reads the first byte in the input stream and returns that byte as an int.
 o readChar()
Reads the next input character and returns that character.
 o readDouble()
Precondition: The next input consists of a double value, possibly preceded by white space, but definitely followed by white space.
 o readFloat()
Precondition: The next input consists of a float value, possibly preceded by white space, but definitely followed by white space.
 o readInt()
Precondition: The next input in the stream consists of an int value, possibly preceded by white space, but definitely followed by white space.
 o readLine()
Reads a line of text and returns that line as a String value.
 o readLineBoolean()
Input should consists of a single word on a line, possibly surrounded by white space.
 o readLineDouble()
Precondition: The user has entered a number of type double on a line by itself, except that there may be white space before and/or after the number.
 o readLineFloat()
Precondition: The user has entered a number of type float on a line by itself, except that there may be white space before and/or after the number.
 o readLineInt()
Precondition: The user has entered a number of type int on a line by itself, except that there may be white space before and/or after the number.
 o readLineLong()
Precondition: The user has entered a number of type long on a line by itself, except that there may be white space before and/or after the number.
 o readLineNonwhiteChar()
Reads the first Nonwhite character on a line and returns that character.
 o readLineWord()
Reads the first string of nonwhite characters on a line and returns that string.
 o readLong()
Precondition: The next input consists of a long value, possibly preceded by white space, but definitely followed by white space.
 o readNonwhiteChar()
Reads the next nonwhite input character and returns that character.
 o readWord()
Reads the first string of nonwhite characters and returns that string.

Constructors

 o SavitchIn
 public SavitchIn()

Methods

 o readLine
 public static String readLine()
Reads a line of text and returns that line as a String value. The end of a line must be indicated either by a new line character '\n' or by a carriage return '\r' followed by a new line character '\n'. (Almost all systems do this automatically. So, you need not worry about this detail.) Neither the '\n', nor the '\r' if present, are part of the string returned. This will read the rest of a line if the line is already partially read.

 o readLineWord
 public static String readLineWord()
Reads the first string of nonwhite characters on a line and returns that string. The rest of the line is discarded. If the line contains only white space, then the user is asked to reenter the line.

 o readLineInt
 public static int readLineInt()
Precondition: The user has entered a number of type int on a line by itself, except that there may be white space before and/or after the number. Action: Reads and returns the number as a value of type int. The rest of the line is discarded. If the input is not entered correctly, then in most cases, the user will be asked to reenter the input. In particular, this applies to incorrect number formats and blank lines.

 o readLineLong
 public static long readLineLong()
Precondition: The user has entered a number of type long on a line by itself, except that there may be white space before and/or after the number. Action: Reads and returns the number as a value of type long. The rest of the line is discarded. If the input is not entered correctly, then in most cases, the user will be asked to reenter the input. In particular, this applies to incorrect number formats and blank lines.

 o readLineDouble
 public static double readLineDouble()
Precondition: The user has entered a number of type double on a line by itself, except that there may be white space before and/or after the number. Action: Reads and returns the number as a value of type double. The rest of the line is discarded. If the input is not entered correctly, then in most cases, the user will be asked to reenter the input. In particular, this applies to incorrect number formats and blank lines.

 o readLineFloat
 public static float readLineFloat()
Precondition: The user has entered a number of type float on a line by itself, except that there may be white space before and/or after the number. Action: Reads and returns the number as a value of type float. The rest of the line is discarded. If the input is not entered correctly, then in most cases, the user will be asked to reenter the input. In particular, this applies to incorrect number formats and blank lines.

 o readLineNonwhiteChar
 public static char readLineNonwhiteChar()
Reads the first Nonwhite character on a line and returns that character. The rest of the line is discarded. If the line contains only white space, then the user is asked to reenter the line.

 o readLineBoolean
 public static boolean readLineBoolean()
Input should consists of a single word on a line, possibly surrounded by white space. The line is read and discarded. If the input word is "true" or "t", then true is returned. If the input word is "false" or "f", then false is returned. Uppercase and lowercase letters are considered equal. If the user enters anything else (e.g. multiple words or different words), then, the user is asked to reenter the input.

 o readChar
 public static char readChar()
Reads the next input character and returns that character. The next read takes place on the same line where this one left off.

 o readNonwhiteChar
 public static char readNonwhiteChar()
Reads the next nonwhite input character and returns that character. The next read takes place immediately after the character read.

 o readInt
 public static int readInt() throws NumberFormatException
Precondition: The next input in the stream consists of an int value, possibly preceded by white space, but definitely followed by white space. Action: Reads the first string of nonwhite characters and returns the int value it represents. Discards the first whitespace character after the word. The next read takes place immediately after the discarded whitespace. In particular, if the word is at the end of a line, the next reading will take place starting on the next line. If the next word does not represent an int value, a NumberFormatException is thrown.

 o readLong
 public static long readLong() throws NumberFormatException
Precondition: The next input consists of a long value, possibly preceded by white space, but definitely followed by white space. Action: Reads the first string of nonwhite characters and returns the long value it represents. Discards the first whitespace character after the string read. The next read takes place immediately after the discarded whitespace. In particular, if the string read is at the end of a line, the next reading will take place starting on the next line. If the next word does not represent a long value, a NumberFormatException is thrown.

 o readDouble
 public static double readDouble() throws NumberFormatException
Precondition: The next input consists of a double value, possibly preceded by white space, but definitely followed by white space. Action: Reads the first string of nonwhitespace characters and returns the double value it represents. Discards the first whitespace character after the string read. The next read takes place immediately after the discarded whitespace. In particular, if the string read is at the end of a line, the next reading will take place starting on the next line. If the next word does not represent a double value, a NumberFormatException is thrown.

 o readFloat
 public static float readFloat() throws NumberFormatException
Precondition: The next input consists of a float value, possibly preceded by white space, but definitely followed by white space. Action: Reads the first string of nonwhite characters and returns the float value it represents. Discards the first whitespace character after the string read. The next read takes place immediately after the discarded whitespace. In particular, if the string read is at the end of a line, the next reading will take place starting on the next line. If the next word does not represent a float value, a NumberFormatException is thrown.

 o readWord
 public static String readWord()
Reads the first string of nonwhite characters and returns that string. Discards the first whitespace character after the string read. The next read takes place immediately after the discarded whitespace. In particular, if the string read is at the end of a line, the next reading will take place starting on the next line. Note, that if it receives blank lines, it will wait until it gets a nonwhitespace character.

 o read
 public static int read()
Reads the first byte in the input stream and returns that byte as an int. The next read takes place where this one left off. This read is the same as System.in.read(), except that it catches IOExceptions.


All Packages  Class Hierarchy  This Package  Previous  Next  Index