简体   繁体   中英

Cannot use console method. How can I use it?

I am practicing how to code using the IntelliJ Idea JAVA Editor.

I am trying to use the console method example:

System.console().printf("Hello World'); OR System.console().readLine("Good Morning);

but for some reason I keep getting a error. How can I make it work using the console method?

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM