0
import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {

      Console cnsl = null;
      String name = null;

      try{
         // creates a console object
         cnsl = System.console();

         // if console is not null
         if (cnsl != null) {

            // read line from the user input
            name = cnsl.readLine("Name: ");

            // prints
            System.out.println("Name entered : " + name);
         }      
      }catch(Exception ex){

         // if any error occurs
         ex.printStackTrace();      
      }
   }
}
4
Contributors
3
Replies
15
Views
3 Years
Discussion Span
Last Post by JamesCherrill
2

welcome to daniweb :)

apart from scanner , you can also try out a BufferedReader .
i dont really see console being used for this kind of stuff. any particular reason you're using it ?

also , whatever you choose to use , its a good practice to close the io stream after your done with it. traditionally its done inside a finally() block , but with java 7 , a fancy try with resources block can be used.
you can check it out here .

Edited by somjit{}

2

How are you running the program? In particular, if you run it with javaw.exe then there is no console. If so, run it with java.exe instead.

Votes + Comments
:)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.