Basic Printing and Keyboard IO


Basic Printing and Keyboard I/O

Java has a powerful and sophisticated I/O capability, but sometimes you only want to read keyboard input and/or write things to the console. To facilitate this, Java provides three built-in data streams to handle standard input, output, and error. Standard input defaults to keyboard input. Standard output defaults to printing to the console. Standard error is an unbuffered output stream that writes to the console. In this section we'll focus on the standard input and output streams.

The standard input and output data streams are implemented as public static fields defined in the System class. A discussion on what a static field is can be found in Chapter 8. The names of the fields are System.in and System.out . Let's first discuss the standard output stream.

System.out is an instance of the PrintStream class, one of the Java I/O classes. System.out has two basic printing methods ” print() and println() . We've already seen the println() method in action in many of the examples in this chapter. The print() and println() methods write a String (a collection of characters ) to the invoking output stream. For instance, the syntax

 System.out.println("Hello there everybody"); 

will display the text "Hello there everybody" on your console. If the value passed to the print() or println() methods is a primitive data type or non- String object the value is converted into a String representation. The difference between the print() and println() methods is that println() adds a newline character to the end of the String whereas print() does not.

Table 6.9. Commonly Used Escape Sequences

E SCAPE SEQUENCE

M EANING

\'

Single quote

\"

Double quote

\\

Backslash

\n

Newline

\t

Tab

Both methods take a String as their argument. You can pass the methods a simple String or a concatenated String made up from two or more pieces. An easy way to concatenate Strings is by using the + operator.

 String name = "Jackson"; int age = 7; System.out.println("Student name: "+name+" Age = "+age); 

As we see in the example code snippet, a primitive datatype can also be concatenated to a String . The value of the integer variable is converted to a String representation before it is concatenated to the String .

Because double quotes surround a String literal definition, you might ask yourself how to represent a String that contains a double quote character. Java defines a number of escape sequences that represent special characters. Some of the commonly used escape sequences are shown in Table 6.9.

Now, let's discuss the standard input stream. The standard input stream defaults to keyboard input. The standard input stream is represented by an instance of the InputStream class named System.in . The InputStream class is designed to read byte data (as compared to character data). System.in can read keyboard input by invoking the read() method. This method reads one or more bytes from an input stream.

Reading console data as bytes, then converting the bytes into characters can be tedious . It is more convenient to wrap a character input stream around the standard input stream. The wrapped stream can then read the keyboard input either as individual characters or as a String . An example is shown in the next section. More information on the Java I/O classes can be found in Chapter 25.

Example: Reading Console Input

As we discussed previously, the standard input stream is a byte stream. It reads data as bytes. When reading console input, it is more convenient to read the data as characters. An InputStreamReader is a character input stream. If we wrap an InputStreamReader around the standard input stream, we can read console input as characters.

We also wrap a BufferedReader around the InputStreamReader because a BufferedReader can read an entire line of data at once. An InputStreamReader will read the data character by character. The BufferedReader object calls the readLine() method that waits until the Enter or Return key is clicked. It then returns the characters that were typed in previously as a String.

I/O operations can throw exceptions. Because of this we place the read operation inside a try block. Java exception handling is discussed in detail in Chapter 12. Note also the use of the standard output stream in this example calling both the print() and println() methods.

 import java.io.*; public class StdIODemo {   public static void main(String args[]) {     String name;     System.out.print("Enter name:  ");     try {       BufferedReader reader =          new BufferedReader(              new InputStreamReader(System.in));       name = reader.readLine();       System.out.println("name was "+name);      } catch (IOException ioe) {}   } } 

Output (will vary) ”

 name was Lisa 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net