2
import java.io.*;

public class ConsoleDemo {

public static void main(String[] args) {
    String str;
    Console con;

    con = System.console();

    if (con == null)
        return;

    str = con.readLine("Enter a string :    ");
    con.printf("Here is your string %s", str);
  }
}

I copied this code from the book, which says that I would get a prompt on the screen for entering a string, but my IDE just gives the message that the execution has termination, without giving me a prompt.

rIshab1988
  • 125
  • 1
  • 4
  • 16
  • You will need to do something with `System.in` as this is the console input channel (the inverse of `System.out` which you can use for printing text on the console) – Joost Jun 11 '12 at 18:22
  • 2
    if(con==null) return; This is the only way to exit without seeing output... So check for null and print out that it is null, then you can ask the next question: Why does System.console() return null when running in ? – Steve H. Jun 11 '12 at 18:24
  • @SteveH. Any way to assign the console a non null value? – rIshab1988 Jun 11 '12 at 18:26

5 Answers5

2

Eclipse nor Netbeans supports the use of Console. The Console.istty() method will return false and you will not have a console to use.

You can change your code to the following and achieve the same result and be able to run it from within the IDE.

import java.io.*; 

public class ConsoleDemo { 

    public static void main(String[] args) { 
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a String and press enter");
        System.out.println("You entered the String: " + scan.nextLine()
    } 
} 
Matt Daley
  • 115
  • 1
  • 2
  • 10
ChadNC
  • 2,528
  • 4
  • 25
  • 39
1

What IDE are you using? This code works just fine when you run it from the command line, so the problem clearly lies with the configuration of your IDE.

Use the following commands to compile and run your code from the command line:

javac ConsoleDemo.java
java ConsoleDemo

Edit: as this link suggests, using System.Console doesn't always work in IDEs. Alternatively you can just use System.in.

Matt Daley
  • 115
  • 1
  • 2
  • 10
1

Your code is working from both Eclipse and Command Prompt.

Try this way as well if you are using Java 5 or +

Scanner in = new Scanner(System.in);
System.out.print("Enter a String : ");
String string = in.nextLine();
System.out.println("Here is your String : " + string);
rizzz86
  • 3,862
  • 8
  • 35
  • 52
0

It is because your IDE runs this code by javaw.exe (windowless -> no console) not java.exe (with console window) command, so System.console() returns null.

Standard solution is to read data from input stream which is represented by System.in so you can use for instance Scanner like

Scanner keybord = new Scanner(System.in);
String line = keybord.readLine();
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • I'm using Eclipse IDE. Any way out of it? – rIshab1988 Jun 11 '12 at 18:31
  • I never got Eclipse to handle the System.console properly -- not sure if it is to do with javaw or java but that never worked for me inside Eclipse itself. – Liv Jun 11 '12 at 18:33
0

By default, eclipse does not associate console with the JVM. You may have to configure it. But if you run it in command line, it will have console definitely and hence it will run without any problem.

Jimmy
  • 2,589
  • 21
  • 31