Saturday, June 9, 2012

Methods of Console IO in Java

In this Post we shall study the methods of Console IO in Java. We shall examine 4 different methods for doing so.However, first of all we require some background information:-

The Standard Input, Standard Output & Standard Error

As , we all know every process is given three open streams by the operating system. Namely , Standard Input,Standard Output and Standard Error.

Standard Input:- The standard input is known as stdin in C, cin in C++ and System.in in Java. By default it is the keyboard but it can be reassigned. System.in is an object of type java.io.InputStream and provides a method read(), which reads the next int from the input source. It returns -1 on End Of File (EOF). From the keyboard we can simulate EOF by pressing CTRL + Z .This will show ^Z on the screen.

Standard Output:- The standard output is known as stdout in C,cout in C++ and System.out in Java. By default it is the monitor but it can be redirected. It is represented in Java by an object of type java.io.PrintStream.

Standard Error:- The standard error is known as stderr in C,cerr in C++ and System.err in Java. By default it is the monitor but it can be redirected. It is represented in Java by an object of type java.io.PrintStream.


Reading Input Directly from the Standard Input using System.in

DirectIO.java

public class DirectIO
{

public static void main(String[] args)
{
//Enter the testing code here
}
public static int readInt()
{
return Integer.parseInt(readLine());
}



public static String readString()
{
try
{
String s="";
while(true)
{
int n=System.in.read();
if(n==-1 || n==13 || n==' ')
return s.trim();
char ch=(char)n;
s=s+ch;
}
}
catch(Exception ex)
{
return "";
}
}









public static String readLine()
{
try
{
String s="";
while(true)
{
int n=System.in.read();
if(n==-1 || n==13)
return s.trim();
char ch=(char)n;
s=s+ch;
}
}
catch(Exception ex)
{
return "";
}
}
}

Explanation: In the the readLine and readString methods we simply read the next input from System.in. This is an int and is stored in n. If n is 13 we have <Enter> being pressed , while n==-1 means an EOF. In both cases we return the current Line. Till these conditions are met , we simply append the next character to the input.
For readString, we also check for the space character ' '.

For readInt, we simply convert the already read Line or String to the required type.

For reading and printing a line use:-
String str=readLine();
System.out.println(str);
For reading and printing an int use:-
int str=readInt();
System.out.println(str);

For reading and printing a Stringuse:-
String str=readString();
System.out.println(str);
For reading and printing a list of Strings :-


String str=readString();
while(str!="")
{
System.out.println(str);
str=readString();
}




Using the java.util.Scanner class
The Scanner class in java.util is a very versatile instrument for text input. It can read all types of data , and from a variety of data sources.The Scanner essentially has two groups of methods:-



1) The hasNextType group. This group returns true if some input of the given type is available and returns false otherwise.
Some functions are hasNext for Objects, hasNextLine for Lines,hasNextInt for integers, and so on.


2) The hasType group. This group returns the next type element from the input source.Some examples are:-

next reads Object, nextLine reads Line, nextInt reads Integers and so on.

Here is some sample code:-

import java.util.Scanner;
public class ScannerIO
{
public static void main( String[] args)
{
Scanner s=new Scanner(System.in);
int a =s.nextInt();
System.out.println(a);
}
}


To be continued...



No comments:

Post a Comment