Java Reference
In-Depth Information
// Wrap the reader with a buffered reader.
BufferedReader buf - in = new BufferedReader (reader);
// Obtain a 16 bit stream.
OutputStreamWriter writer =
new OutputStreamWriter (System.out);
// Wrap the PrintWriter stream around the output stream
PrintWriter print - writer =
new PrintWriter (writer, true);
String str ="q";
try {
// Read a whole line a time. Check the string for
// the "quit" input to jump from the loop.
do {
// Read text from keyboard
str = buf - in.readLine ();
// PrintWriter output.
print - writer.println ( " echo " + str);
} while (!str.toLowerCase ().equals ( " q " ));
}
catch (IOException e) {
System.out.println ("IO exception ="+e);
}
} // main
} // class BufferedReaderApp
A session with the program produces output like this:
My input from the keyboard
echo My input from the keyboard
More of my input
echo More of my input
q
echo q
To obtain numerical values from the keyboard requires reading them in as strings
and then converting them to primitive types. For example, the following snippet
will read in an integer value as a string and then convert it to an int value:
InputStreamReader reader = new InputStreamReader (System.in);
BufferedReader buf - reader = new BufferedReader (reader);
Search WWH ::




Custom Search