Updates

Friday 16 August 2013

How To Read Input From Console in Java (Part 1)

Accepting Input from the Keyboard

A stream is required to accept input from the keyboard. A stream represents flow of data from one
Place to another place. It is like a water-pipe where water flows.  Like a water-pipe carries water from
One place to another, a stream carries data from one place to another place .A stream can carry
Data . From keyboard to memory or from memory to printer or from memory to a file. A stream is
Always required if we want to move data from one place to another.

 Basically, there are two types of streams: input streams and output streams.

 Input streams are those streams which receive or read data coming from some other place. Output streams  are those Streams which sender write data to some other place.
All streams are represented by classes in java.io (input and output) package. This package
Contains a lot of classes, all of which can be classified into two basic categories :input streams and
Outputs treams.
Keyboard  is represented by a field, called in System class. When we write System.in, we are
Representing a standard input device, i.e. keyboard ,by default. System class is found in java.lang
(language) package and has three fields' as shown below. All these fields represent some type of
stream:
    System.in: This represents Ii1putStream object ,which by default represents standard input       Device. eg. keyboard.
    System.out: This represents PrintStream object ,which by default represents standard output device, eg. monitor.
     System.err: This field also represents PrintStream object, which by default represents monitor.
Note: that both System.out and System.err can be used to represent the monitor rand hence any of
These two can be used to send data to the monitor.

What is the difference between System.out and System.err?

System.out and System.err both represent the monitor by default and hence can be used to send
Data or results to the monitor .But System.out is used to display normal messages and results whereas
System.err is used to display error messages.
 System.out.println("A normal message");
 System.err.println("An error Message");

To accept data from the keyboard ,eg . System.in , we need to connect it to an input stream as some
. inputstream is needed to read data.


We will discuss the ways to input a String, an integer, and a real number. System.in is a pre-defined InputStream used with the console. Unfortunately this class does not have a method for simple input. But there is a java.io.BufferedReader class that has a method readLine(). A buffer is a region in memory where input from the terminal is stored until needed by the program. If you want to perform buffered input on the System.in stream you would pass the System.in object into the constructor

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.
For top efficiency, consider wrapping an InputStreamReader within a BufferedReader.
BufferedReader input = new
BufferedReader (new InputStreamReader (System.in));

Once we have created a BufferedReader we can use its method readLine() to read one line of characters at a time from the keyboard and store it as a

String object.
String inputString = input.readLine(); 
Once we have the input data in a String object we can use the various methods
available to a String object to manipulate the data.  Since data can get lost/corrupted
during an input operation the method readLine() canalert the user by
throwing an exception. There are several ways that Java allows
a user to handle exceptions. For the time being we will add the phrase "throws
IOException" and let the system to do the error handling.

Reading
an integerOne way to read an integer is to read the input as a String and then use the method parseInt() of the wrapper class Integer to convert the String to an integer.
int num = Integer.parseInt ( inputString);

a real numberThere is a wrapper class java.lang.Double that can take
the input as a String and a method parseDouble() to convert the String
to a real number.

double d = Double.parseDouble (inputString );

An Example :

import java.io.*;
public class CalcArea
{
  public static void main ( String args[] ) throws IOException
  {
    System.out.print ( "Enter the radius: " );
    BufferedReader input = new BufferedReader ( new InputStreamReader ( System.in ) );
    String inputString = input.readLine();

    double radius = Double.parseDouble ( inputString );
    double area = 3.14159 * radius * radius;

    System.out.println ( "Area is: " + area );
  }
}

No comments:

Post a Comment