我有以下问题:方法readLine()或nextLine()、nextInt()等等抛出一个异常:NullPointerException。
我使用NetBeans IDE (如果重要的话)。
public static void Reading()
{
String qq;
qq = System.console().readLine();
System.console().printf(qq);
}发布于 2014-09-27 19:03:51
有些IDE不提供控制台。注意,在这些情况下,System.console()返回null。
来自文献阴离子
返回: 系统控制台(如果有的话)否则为空。
您可以始终使用System.in和System.out,如下所示:
String qq;
Scanner scanner = new Scanner(System.in);
qq = scanner.nextLine();
System.out.println(qq);发布于 2014-09-27 19:04:34
有两件事:
System.out.println("Thing to print");Scanner s = new Scanner(System.in); String input = s.nextLine();因此,考虑到这些,您的代码应该是
public static void Reading() {
String qq;
Scanner s = new Scanner(System.in);
qq = s.nextLine();
System.out.println(qq);
s.close();
}或
public static void Reading() {
String qq;
try (Scanner s = new Scanner(System.in)) {
qq = s.nextLine();
System.out.println(qq);
}
}https://stackoverflow.com/questions/26078106
复制相似问题