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();      
      }
   }
}

Recommended Answers

All 3 Replies

you could try using the scanner class.
does it prompt you for input?

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 .

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.

commented: :) +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.