Home | | Web Programming | Reading Console Input - Java

Chapter: Java The Complete Reference : The Java Language : I/O, Applets, and Other Topics

Reading Console Input - Java

In Java 1.0, the only way to perform console input was to use a byte stream. Today, using a byte stream to read console input is still acceptable.

Reading Console Input

In Java 1.0, the only way to perform console input was to use a byte stream. Today, using a byte stream to read console input is still acceptable. However, for commercial applications, the preferred method of reading console input is to use a character-oriented stream. This makes your program easier to internationalize and maintain.

 

In Java, console input is accomplished by reading from System.in. To obtain a character-based stream that is attached to the console, wrap System.in in a BufferedReader object. BufferedReader supports a buffered input stream. A commonly used constructor is shown here:

 

BufferedReader(Reader inputReader)

 

Here, inputReader is the stream that is linked to the instance of BufferedReader that is being created. Reader is an abstract class. One of its concrete subclasses is InputStreamReader, which converts bytes to characters. To obtain an InputStreamReader object that is linked to System.in, use the following constructor:

 

InputStreamReader(InputStream inputStream)

 

Because System.in refers to an object of type InputStream, it can be used for inputStream. Putting it all together, the following line of code creates a BufferedReader that is connected to the keyboard:

BufferedReader br = new BufferedReader(new

 

InputStreamReader(System.in));

 

After this statement executes, br is a character-based stream that is linked to the console through System.in.

 

Reading Characters

 

To read a character from a BufferedReader, use read( ). The version of read( ) that we will be using is

 

int read( ) throws IOException

 

Each time that read( ) is called, it reads a character from the input stream and returns it as an integer value. It returns –1 when the end of the stream is encountered. As you can see, it can throw an IOException.

The following program demonstrates read( ) by reading characters from the console until the user types a "q." Notice that any I/O exceptions that might be generated are simply thrown out of main( ). Such an approach is common when reading from the console

in simple example programs such as those shown in this book, but in more sophisticated applications, you can handle the exceptions explicitly.

 

// Use a BufferedReader to read characters from the console.

import java.io.*;

 

class BRRead {

 

public static void main(String args[]) throws IOException

 

{

 

char c;

 

BufferedReader br = new

 

BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter characters, 'q' to quit.");

 

// read characters

do {

 

c = (char) br.read(); System.out.println(c);

} while(c != 'q');

 

}

 

}

 

Here is a sample run:

 

Enter characters, 'q' to quit. 123abcq

 

1

 

2

 

3 a b c q

 

This output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press enter. As you can guess, this does not make read( ) particularly valuable for interactive console input.

 

Reading Strings

 

To read a string from the keyboard, use the version of readLine( ) that is a member of the BufferedReader class. Its general form is shown here:

 

String readLine( ) throws IOException

 

As you can see, it returns a String object.

 

The following program demonstrates BufferedReader and the readLine( ) method; the program reads and displays lines of text until you enter the word "stop":

 

// Read a string from console using a BufferedReader.

import java.io.*;

class BRReadLines {

 

public static void main(String args[]) throws IOException

 

{

 

// create a BufferedReader using System.in

BufferedReader br = new BufferedReader(new

 

InputStreamReader(System.in));

 

String str;

 

System.out.println("Enter lines of text.");

System.out.println("Enter 'stop' to quit."); do {

 

str = br.readLine(); System.out.println(str);

} while(!str.equals("stop"));

 

}

 

}

The next example creates a tiny text editor. It creates an array of String objects and then reads in lines of text, storing each line in the array. It will read up to 100 lines or until you enter "stop." It uses a BufferedReader to read from the console.

 

// A tiny editor.

import java.io.*;

 

class TinyEdit {

 

public static void main(String args[]) throws IOException

 

{

 

// create a BufferedReader using System.in

BufferedReader br = new BufferedReader(new

 

InputStreamReader(System.in)); String str[] = new String[100];

System.out.println("Enter lines of text.");

System.out.println("Enter 'stop' to quit.");

 

for(int i=0; i<100; i++) { str[i] = br.readLine();

if(str[i].equals("stop")) break;

}

 

System.out.println("\nHere is your file:"); // display the lines

 

for(int i=0; i<100; i++) {

if(str[i].equals("stop")) break; System.out.println(str[i]);

}

 

}

 

}

 

Here is a sample run:

 

Enter lines of text.

 

Enter 'stop' to quit.

 

This is line one.

 

This is line two.

 

Java makes working with strings easy.

 

Just create String objects.

stop

 

Here is your file: This is line one. This is line two.

 

Java makes working with strings easy. Just create String objects.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Language : I/O, Applets, and Other Topics : Reading Console Input - Java |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.