Reading from Standard Input

J.E.D.I. java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment java.endorsed.dirs=C:\Program Files\Java\jdk1.5.0_06\jre... os.arch=x86 java.io.tmpdir=C:\DOCUME~1\becca\LOCALS~1\Temp\ line.separator= java.vm.specification.vendor=Sun Microsystems Inc. user.variant= os.name=Windows XP sun.jnu.encoding=Cp1252 java.library.path=C:\Program Files\Java\jdk1.5.0_06\jre... java.specification.name=Java Platform API Specification java.class.version=49.0 sun.management.compiler=HotSpot Client Compiler os.version=5.1 user.home=C:\Documents and Settings\becca user.timezone= java.awt.printerjob=sun.awt.windows.WPrinterJob file.encoding=Cp1252 java.specification.version=1.5 user.name=becca java.class.path=C:\Documents and Settings\becca\Neste... java.vm.specification.version=1.0 sun.arch.data.model=32 java.home=C:\Program Files\Java\jdk1.5.0_06\jre java.specification.vendor=Sun Microsystems Inc. user.language=en awt.toolkit=sun.awt.windows.WToolkit java.vm.info=mixed mode, sharing java.version=1.5.0_06 java.ext.dirs=C:\Program Files\Java\jdk1.5.0_06\jre... sun.boot.class.path=C:\Program Files\Java\jdk1.5.0_06\jre... java.vendor=Sun Microsystems Inc. file.separator=\ java.vendor.url.bug=http:java.sun.comcgi-binbugreport... sun.cpu.endian=little sun.io.unicode.encoding=UnicodeLittle sun.desktop=windows sun.cpu.isalist=

5.3 Reading from Standard Input

Instead of getting user input from the command-line, most users prefer inputting data when prompted by the program while it is already in execution. One way of doing this is with the use of streams. A stream is an abstraction of a file or a device that allows a series of items to be read or written. Streams are connected to physical devices such as keyboards, consoles and files. There are two general kinds of streams, byte streams and character streams. Byte streams are for binary data while character streams are for Unicode characters. System.in and System.out are two examples of predefined byte streams in java. The first one by default refers to the keyboard and the latter refers to the console. To read characters from the keyboard, you can use the System.in byte stream warped in a BufferedReader object. The following line shows how to do this: BufferedReader br = new BufferedReadernew InputStreamReaderSystem.in; Introduction to Programming II Page 74 J.E.D.I. The read method of the BufferedReader object is then used to read from the input device. ch = int br.read; read method returns an integer Try out this sample code. import java.io.; class FavoriteCharacter { public static void mainString args[] throws IOException { System.out.printlnHi, whats your favorite character?; char favChar; BufferedReader br = new BufferedReadernew InputStreamReaderSystem.in; favChar = char br.read; System.out.printlnfavChar + is a good choice; } } Running this code with the character a as input generates the following output: Hi, whats your favorite character? a a is a good choice If you prefer reading an entire line instead of reading one character at a time, you can use the readLine method. str = br.readLine; Here is a program almost similar to the preceding example but reads an entire string instead of just one character. import java.io.; class GreetUser { public static void mainString args[] throws IOException { System.out.printlnHi, whats your name?; String name; BufferedReader br = new BufferedReadernew InputStreamReaderSystem.in; name = br.readLine; System.out.printlnNice to meet you, + name + :; } } Here is the expected output of GreetUser when the user inputs rebecca: Hi, whats your name? rebecca Nice to meet you, rebecca : When using streams, dont forget to import the java.io package as shown below: import java.io.; One more reminder, reading from streams may cause checked exceptions to occur. Dont forget to handle these exceptions using try-catch statements or by indicating the Introduction to Programming II Page 75 J.E.D.I. exception in the throws clause of the method.

5.4 File Handling