Java Reference
In-Depth Information
public static final PrintStream out; // Standard output
public static final PrintStream err; // Standard error output
Note that PrintStream extends FileOutputStream, which extends
OutputStream. PrintStream adds functionality by allowing the display of
various data types. In addition, PrintStream contains the println()
method, which we have often used in preceding chapters. This method
adds a newline character (\n) at the end of the string or array and auto-
maticallyflushesthestream.
The standard streams are always open and ready for use. This makes
them convenient for Java console applications, such as the ones devel-
opedpreviouslyinthisbook.
The Keyin Class
ThusfarinthisbookwehaveusedaclassnamedKeyintoobtainconsolein-
putforcharacterandnumerictypes.
TheKeyinclasscontainssixstaticmethods,asfollows:
1. inString()allowsthepromptlessinputofastring.Thismethodisusedinter-
nallybytheclasstoobtaintheindividualcharactersinanintordoublevari-
able.
2. InputFlush()ensuresthatthereisnodataavailableintheinputstream.If
data is found, the read() method is called to remove it. InputFlush() is
calledbythedatainputmethodsintheKeyinclass.
3. inString(Stringprompt)isusedtoinputauserstring.Thestringpassedas
anargumentisdisplayedasaprompt.
4. inInt(Stringprompt)allowstheinputofaninttypevalue.Thestringpassed
asanargumentisdisplayedasaprompt.
5. inChar(Stringprompt)allowstheusertoinputasinglevalueoftypechar.
Thestringpassedasanargumentisdisplayedasaprompt.
6. inDouble(Stringprompt)allowstheusertoinputafloating-pointvalueand
returnsitasavalueoftypedouble.Thestringpassedasanargumentisdis-
playedasauserprompt.
All methods of the Keyin class catch some of the exceptions raised by
the read() method of the InputStream class. Exceptions are discussed in
Chapter 19. The methods that input character data (inString and inChar)
catch IOException. The methods that input numbers in int and float for-
matcatchtheNumberFormatException.
Search WWH ::




Custom Search