我使用以下简单代码从控制台读取一行字符串但得到nullpointerexception,你能帮忙:
import java.io.Console;
public class readline_String {
public static void main(String[] args)
{
// TODO Auto-generated method stub
//String str=System.console().readLine();
System.out.println("Enter an input string:");
Console c=System.console();
String str=c.readLine();
System.out.println("The input string is:");
System.out.println(str);
}
}
答案 0 :(得分:2)
用以下代码替换您的代码:
System.out.println("Enter an input string:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println("The input string is:");
System.out.println(str);
答案 1 :(得分:2)