UserInputStream
User's Guide
Purpose
In Java reading user input in a console application is difficult at
best. UserInputStream provides the means to easily read keyboard
input and interpret it as Java primitive data types. This class also
shields the programmer from the tedium of catching numerous exceptions
for each read when reading from System.in.
Usage
constructor
- UserInputStream(InputStream i)
- Creates an instance of UserInputStream which operates on i.
methods
- int readInt()
- Reads the next "word" in the stream and attempts to return it as
an int.
- int readlnInt()
- Same as readInt(), except that the next read will start at the
beginning of the next line of input.
- float readFloat()
- Reads the next "word" in the stream and attempts to return it as a
float.
- float readlnFloat()
- Same as readFloat(), except that the next read will start at the
beginning of the next line of input.
- double readDouble()
- Reads the next "word" in the stream and attempts to return it as a
double.
- double readlnDouble()
- Same as readDouble(), except that the next read will start at the
beginning of the next line of input.
- char readChar()
- Returns the next char in the stream.
- char readlnChar()
- Returns the next char in the stream. The next read will take
place at the start of the next line of input.
- String readString()
- Returns the next "word" in the stream.
- String readlnString()
- Returns the next "word" in the stream. The next read will take
place at the start of the next line of input.
- void readln()
- Forces the next read to start at the beginning of the next line of
input.
NOTE: Words refer to groupings of characters separated by blanks
and or newline characters.
example:
import java.io.*;
import UserInputStream;
class Ex {
private UserInputStream input;
private String name;
private int age;
Ex() {
input = new UserInputStream(System.in);
System.out.println("What is your name?");
name = input.readlnString();
System.out.println("How old are you?");
age = input.readInt();
System.out.print("Hello "+name+", you are ");
System.out.print(age);
System.out.println(" years old.");
}
public static void main(String argv[]) {
new Ex();
}
}