java code for reading input from console


import java.io.Console;
public class MainClass
{
  public static void main(String[] args) throws ClassNotFoundException
   {
    Console console = System.console();
    if (console == null)
  {
      System.err.println("sales: unable to obtain console");
      return;
    }

    String username = console.readLine("Enter username: ");
    System.out.println(username);
  }
}


We can read Java’s input from “System.in” console, there are two common ways to read input from console.

1) InputStreamReader wrapped in a BufferedReader
2) Scanner classes in JDK1.5

Output


C:\pgm\console>java MainClass
 Enter username: vineeth
vineeth


Here is the java program for read string from console application.

0 comments:

Post a Comment

 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org