Choose Your Language

Monday 15 July 2013

java program to read input from console

import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {    
     Console console = null;
     try{
         console = System.console();
          if (console != null) {
            String name  = console.readLine("Name: ");          
            char[] pwd = console.readPassword("Password: ");
            System.out.println("Name is: " + name);        
            System.out.println("Password is: "+pwd);
            System.out.println("Password is: "+new String(pwd));
          }    
      }catch(Exception ex){
         ex.printStackTrace();    
      }
   }
}

output:
Name: aravind
Password:
Name is: aravind
Password is: [C@1a758cb
Password is: sankaran

No comments:

Post a Comment