by Dinesh Thakur

Java.io.Console class is a new class in Java 6 that may be used for reading text from character based console device. The data can be read from the console device provided it is associated with the Java Virtual Machine (JVM).

To determine whether console device is present in the current JVM, we invoke Console() method of System class and if this method returns a Console reference, that means the console device is present in current JVM as shown in following code:

If console device is present in current JVM, we get a console reference which can be used to call the Console class's methods.

 

Here is the Java Example for Console I/O:

import java.io.*;

public class ConsoleIO
{
            public static void main(String[] args)
           {
                        int p,q,r;
                        float a;
                        String val;
                        Console console=System.console();
                        val=console.readLine("Enter first value ");
                        p=Integer.parseInt(val);
                        val=console.readLine("enter second value ");
                        q=Integer.parseInt(val);
                        val=console.readLine("Enter third value ");
                        r=Integer.parseInt(val);
                        a=(float)(p+q+r)/3;
                        System.out.println("Average of three variables is "+a);
            }
}