扫描器如何决定等待用户输入以及为什么它在使用toString时不等待输入?

huangapple go评论61阅读模式
英文:

How Scanner decides to wait for user input & why it doesn't wait for input with toString?

问题

我试图通过使用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类中定义的标准方法。

有人可以帮忙理解为什么程序不会等待用户输入吗?

英文:

I was trying to capture the user input(a String) by using a Scanner object to process it further as required, while doing that I tried using a method which is not listed in standard methods of String class.

So my code looked like below :

    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);

using .toString does not throw an error at all, but also the program does not wait for the user input.

I'm aware that using a .nextLine can solve the problem here as its a standard method defined for use in Scanner class.

Can someone please help understand, why the program does not wait for the user input ?

答案1

得分: 2

> ...在这样做的同时,我尝试了一种未在String类的标准方法中列出的方法。

Java中的每个类默认都继承了一个称为Object的类。类Object有一个名为toString的方法,它返回一个String。这意味着如果一个类没有覆盖(即重新定义)方法toString,在其对象上调用此方法将打印出Object#toString返回的内容。

> 使用.toString根本不会抛出错误

既然你已经理解了toString的概念,我就不需要向你解释为什么它没有抛出错误。

> 但是程序也不会等待用户输入。

你为此目的调用了错误的方法。为了等待输入,Scanner有一个叫做next的方法,以及以next开头的方法(例如nextLinenextInt等),根据你的要求。我建议你花些时间研究文档。示例用法如下:

String captured_string = user_input.nextLine();
英文:

> ...while doing that I tried using a method which is not listed in
> standard methods of String class.

Every class in Java inherits a class called, Object by default. The class, Object has a method called, toString which returns a String. It means that if a class does not override (i.e. redefine) the method, toString, calling this method on its object will print what Object#toString returns.

> using .toString does not throw an error at all

Now that you have understood the concept of toString, I do not need to explain to you why it did not throw an error.

> but also the program does not wait for the user input.

You have called the wrong method for this purpose. In order to wait for input, Scanner has the method, next and the methods starting with name next (e.g. nextLine, nextInt etc.) as per your requirement. I suggest you spend some time studying the documentation. A sample usage will be as follows:

String captured_string = user_input.nextLine();

答案2

得分: 1

因为toString将scanner对象转换为字符串,所以你需要使用nextLine方法,这就是为什么它会打印出这些奇怪的内容。

英文:

You are required to use nextLine method because toString convert the object scanner to string that's why it prints this weird stuff.

答案3

得分: 0

Scanner.toString()方法返回有关Scanner对象本身的信息,而不是来自您正在使用的输入流的任何数据。请参阅文档:

> 返回此Scanner的字符串表示形式。 Scanner的字符串表示形式包含可能对调试有用的信息。 具体格式未指定。

当您想要从输入流中读取数据时,必须使用任何next*()方法,比如nextLine()

英文:

The Scanner.toString() method returns information about the Scanner object itself, not any data from the input stream you are using. See the documentation:

> Returns the string representation of this Scanner. The string representation of a Scanner contains information that may be useful for debugging. The exact format is unspecified.

When you want to read data from the input stream you have to use any next*() method like nextLine().

huangapple
  • 本文由 发表于 2020年10月4日 20:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64194740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定