Don't use the System.console()
like that, instead
System.out.println("Hello World");
or
System.out.printf("%s%n", "Hello World");
And to read from System.in
, I would suggest aScanner
; like
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a line: ");
String line = scan.nextLine();
System.out.println("You entered: " + line);
The reason I would avoid System.console()
is documented in the Javadoc which notes whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked.
Console cons = System.console();
if (cons != null) {
String line = cons.readLine();
System.out.println(line);
} else {
System.out.println("no console");
}
And in my IDE (as in yours), there is no System.console()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.