Java: How to get input from System.console()

后端 未结 9 1872
时光说笑
时光说笑 2020-11-22 09:08

I am trying to use Console class to get input from user but a null object is returned when I call System.console(). Do I have to change anything before using Sy

9条回答
  •  清歌不尽
    2020-11-22 10:00

    Found some good answer here regarding reading from console, here another way use 'Scanner' to read from console:

    import java.util.Scanner;
    String data;
    
    Scanner scanInput = new Scanner(System.in);
    data= scanInput.nextLine();
    
    scanInput.close();            
    System.out.println(data);
    

提交回复
热议问题