【发布时间】:2021-01-19 11:59:42
【问题描述】:
我试图通过使用 Scanner 对象根据需要进一步处理它来捕获用户输入(字符串),同时我尝试使用未在 String 类的标准方法中列出的方法。
所以我的代码如下所示:
Scanner user_input = new Scanner(System.in);
System.out.println("Please enter the string");
String captured_string = user_input.toString();
System.out.println(captured_string);
使用 .toString 根本不会抛出错误,而且程序也不会等待用户输入。
我知道使用 .nextLine 可以解决这里的问题,因为它是为在 Scanner 类中使用而定义的标准方法。
有人可以帮助理解,为什么程序不等待用户输入?
【问题讨论】:
-
打印什么
-
请输入字符串 java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false] [跳过=false][组分隔符=\x{2c}][小数分隔符=\x{2e}][正前缀=][负前缀=\Q-\E][正后缀=][负后缀=] [NaN 字符串=\Q�\E][无穷大字符串=\Q∞\E]
-
@Simon 请edit您的帖子包含您想问的问题。
标签: java java.util.scanner tostring