- Forward


Console Input and Output in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Understanding Streams
Back SMYC Forward
  • What Are They:
    • A sequence of bytes with indeterminate length that ends with an END-OF-STREAM character (Ctrl-D in Unix and Ctrl-Z in Windows)
  • Why They Are Used:
    • The way you work with a stream is independent of its channel (e.g., keyboard, console, network connection, storage devices)
"Standard" Streams
Back SMYC Forward
  • What Are They?
    • Streams that are associated with (what used to be) the standard channels for input and output
  • What Does This Mean Now?
    • The terminal window (sometimes called the console) has standard streams associated with it
    • Most IDEs have standard streams associated with one or more windows/panes/views
"Standard" Streams in Java
Back SMYC Forward
  • When Are They Created?
    • Before main() is called
  • What Are They Named?
    • System.in
    • System.out
    • System.err
A Brief Quiz
Back SMYC Forward
  • Some Questions (Related to Output):
    • What is System java.lang.System (and how can you tell)?
    • What is System.out (and how can you tell)?
    • What is System.err (and how can you tell)?
  • Some Questions (Related to Input):
    • What is System java.lang.System (and how can you tell)?
    • What is System.in (and how can you tell)?
    • What methods does System.in have (and not have)?
  • Some Questions Related to Both:
    • How would you use System.out, System.err, and/or System.in?
Encapsulation of Standard Streams
Back SMYC Forward
  • Standard Output/Error:
    • System.out and System.err are PrintStream java.io.PrintStream objects
  • Standard Input:
    • System.in is an InputStream java.io.InputStream object
Using a PrintStream Object
Back SMYC Forward
  • Unformatted Output:
    • PrintStream.print(java.lang.String) java.io.PrintStream#print(java.lang.String) prints the String (i.e., sends it to the stream)
    • PrintStream.println(java.lang.String) java.io.PrintStream#println(java.lang.String) prints the String followed by the line separator String (which is "\n" in Linux, "\r" in OSX and "\r\n" in MSW)
  • An Important Point:
    • Text sent to an output stream may be buffered
    • To force text through the buffer use PrintStream.flush() java.io.PrintStream#flush()
    • This is particularly important to be aware of when debugging
Using a PrintStream Object (cont.)
Back SMYC Forward
  • An Observation:
    • Output often needs to be formatted in a very particular way
  • Format Strings:
    • Encapsulated in the Formatter java.util.Formatter class
  • Two Approaches to Using Format Strings:
    • Use the String.format(java.lang.String, java.lang.Object...) java.lang.String#format(java.lang.String, java.lang.Object...) method to create a formatted String and then call print() or println()
    • Use the PrintStream.printf(java.lang.String, java.lang.Object...) java.io.PrintStream#printf(java.lang.String, java.lang.Object...) convenience method to perform both steps in one call
The Format String
Back SMYC Forward
  • Components:
    • String literals
    • Format specifiers
  • Format Specifiers:
    • Syntax: %[flags][width][.precision]conversion
  • Conversions:
    • 'b' for a boolean
    • 'c' for a char
    • 'd' for an integer
    • 'e' for a real number in scientific notation
    • 'f' for a real number
    • 's' for a String
  • Flags:
    • '-' for left-justified
    • '+' to always include the sign
    • ' ' to use a space for the sign of a positive number
    • '0' to pad with zeros
    • ',' to use grouping separators
    • '(' to put negative numbers in parentheses
Using a PrintStream Object (cont.)
Back SMYC Forward

An Example that uses printf()

javaexamples/iobasics/PrintfExample.java
 
Using an InputStream Object
Back SMYC Forward
  • Raw Input:
    • The read() methods can be used to read one or more bytes
  • An Observation:
    • We don't want to have to work with the binary representation of char, double, or int
  • A Better Approach:
    • "Wrap" the InputStream in a Scanner java.util.Scanner object and use its next___() methods
Using a Scanner Object
Back SMYC Forward
  • Important Constructors:
    • Scanner(java.io.InputStream) java.util.Scanner#Scanner(java.io.InputStream)
  • Some Relevant Methods:
    • Scanner.useDelimiter(java.lang.String) java.util.Scanner#useDelimiter(java.lang.String)
    • Scanner.hasNextDouble() java.util.Scanner#hasNextDouble() , Scanner.hasNextInt() java.util.Scanner#hasNextInt() , and Scanner.hasNextLine() java.util.Scanner#hasNextLine()
    • Scanner.nextDouble() java.util.Scanner#nextDouble() , Scanner.nextInt() java.util.Scanner#nextInt() , and Scanner.nextLine() java.util.Scanner#nextLine()
Using a Scanner Object (cont.)
Back SMYC Forward

An Example that uses a Scanner

javaexamples/iobasics/ScannerExample.java
 
Processing Input with a Scanner (cont.)
Back SMYC Forward

An Inconvenience when Using Scanner Objects

javaexamples/basics/ScannerInconvenienceExample.java
 
Using a Scanner Object (cont.)
Back SMYC Forward
  • The Issue:
    • The methods that read "up to" a delimiter do not consume newline characters
  • A "Work Around":
    • Invoke nextLine() immediately after invoking one of these methods
The "Modern" Approach to Console I/O
Back SMYC Forward
  • A Recent Addition:
    • A Console object can be retrieved using the System.console() method and it has both readLine() and printf() methods
  • Problems with this Approach:
    • The console() method returns null in IDEs
    • The streams can't be re-directed within a program making it inappropriate for many testing environments
There's Always More to Learn
Back -