Thursday, August 26

Reading input from console in Java

If you are new to Java then you may be thinking of reading input from console. I have seen many people being able to take input in swing framework, but the most basic thing, reading input from console as in C/C++, they gets bewildered.. :D
So today I think of updating my blog with the basic idea of reading input from console which is very simple.Here I have implemented console class for this purpose.

The complete java code looks like this:
import java.io.Console;
/**
 *
 * @author 0v3rr!d3
 */
public class console {

    public static void main(String []a)
    {
        Console conn;

        conn=System.console();

        if (conn == null)
        {
            System.out.println("No console available");
        }

        String name;
        name=conn.readLine("Name: ");

        System.out.println("You typed: " + name);
    }
}


The above program takes input name from the user and then displays what the user has typed. Enjoy it... Please leave a comment if you experienced any problem.. regarding this..

No comments:

Post a Comment