Java 扫描仪在空格后看不到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19509647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:52:09  来源:igfitidea点击:

Scanner doesn't see after space

javajava.util.scanner

提问by igeer12

I am writing a program that asks for the person's full name and then takes that input and reverses it (i.e John Doe - Doe, John). I started by trying to just get the input, but it is only getting the first name.

我正在编写一个程序,该程序要求输入此人的全名,然后获取该输入并将其反转(即 John Doe - Doe, John)。我开始尝试只获取输入,但它只获取名字。

Here is my code:

这是我的代码:

public static void processName(Scanner scanner) {
    System.out.print("Please enter your full name: ");
    String name = scanner.next();
    System.out.print(name);
}

采纳答案by Prabhakaran Ramaswamy

Change to String name = scanner.nextLine();instead of String name = scanner.next();

改为 String name = scanner.nextLine();而不是String name = scanner.next();

See more on documentation here - next()and nextLine()

在此处查看有关文档的更多信息 - next()nextLine()

回答by Wold

scanner.next();takes only the next word. scanner.nextLine();should work. Hope this helps

scanner.next();只需要下一个词。scanner.nextLine();应该管用。希望这可以帮助

回答by Ankur

try using this

尝试使用这个

String name = scanner.nextLine();

回答by PurkkaKoodari

From Scannerdocumentation:

Scanner文档:

A Scannerbreaks its input into tokens using a delimiter pattern, which by default matches whitespace.

AScanner使用分隔符模式将其输入分解为标记,默认情况下与空格匹配。

and

public String next()

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

public String next()

从此扫描器中查找并返回下一个完整的令牌。一个完整的标记前后是与分隔符模式匹配的输入。

This means by default the delimiter pattern is "whitespace". This splits your text at the space. Use nextLine()to get the whole line.

这意味着默认情况下分隔符模式是“空白”。这会在空间中拆分您的文本。使用nextLine()让整条生产线。

回答by Woody

Try replacing your code

尝试替换您的代码

String name = scanner.nextLine();

instead

反而

String name = scanner.next();

next()can read the input only till the space. It can't read two words separated by space. Also, next()places the cursor in the same line after reading the input.

next()只能读取输入直到space. 它无法读取以space.分隔的两个单词。此外,next()在读取输入后将光标置于同一行。

nextLine()reads input including spacebetween the words (that is, it reads till the end of line \n). Once the input is read, nextLine()positions the cursor in the next line.

nextLine()读取包括space单词之间的输入(即读取到行尾\n)。读取输入后,nextLine()将光标定位在下一行。

回答by Pankaj Sonani

 public static void processName(Scanner scanner) {
        System.out.print("Please enter your full name: ");
        scanner.nextLine();
        String name = scanner.nextLine();
        System.out.print(name);
    }

Try the above code Scanner should be able to read space and move to the next reference of the String

试试上面的代码 Scanner 应该能够读取空间并移动到 String 的下一个引用