Java 扫描器类跳过空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18158797/
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-11 22:54:03  来源:igfitidea点击:

Scanner class skips over whitespace

javaarrays

提问by Casper

I am using a nested Scannerloop to extract the digit from a string line (from a text file) as follows:

我正在使用嵌套Scanner循环从字符串行(从文本文件)中提取数字,如下所示:

String str = testString;            
Scanner scanner = new Scanner(str); 
while (scanner.hasNext()) {
    String token = scanner.next();
    // Here each token will be used
}

The problem is this code will skip all the spaces " ", but I also need to use those "spaces" too. So can Scannerreturn the spaces or I need to use something else?

问题是这段代码会跳过所有空格" ",但我也需要使用这些“空格”。那么可以Scanner返回空格还是我需要使用其他东西?

My text file could contain something like this:

我的文本文件可能包含如下内容:

0
011
abc
d2d

sdwq

sda

Those blank lines contains 1 " "each, and those " "are what I need returned.

这些空行各包含 1" "个,这些" "是我需要返回的。

采纳答案by Hovercraft Full Of Eels

Use Scanner's hasNextLine()and nextLine()methods and you'll find your solution since this will allow you to capture empty or white-space lines.

使用 ScannerhasNextLine()nextLine()方法,您将找到您的解决方案,因为这将允许您捕获空行或空白行。

回答by LaurentG

You have to understand what is a token. Read the documentation of Scanner:

您必须了解什么是令牌。阅读以下文档Scanner

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

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

You could use the nextLine()method to get the whole line and not "ignore" with any whitespace.

您可以使用该nextLine()方法来获取整行,而不是“忽略”任何空格。

Better you could define what is a token by using the useDelimitermethod.

更好的是,您可以使用该useDelimiter方法定义什么是令牌。

回答by Ankur Lathi

By default, a scanner uses white space to separate tokens.

默认情况下,扫描仪使用空格来分隔标记。

Use Scanner#nextLinemethod, Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

使用Scanner#nextLine方法,将此扫描器前进到当前行并返回被跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。

To use a different token separator, invoke useDelimiter(), specifying a regular expression. For example, suppose you wanted the token separator to be a comma, optionally followed by white space. You would invoke,

要使用不同的标记分隔符,请调用 useDelimiter(),指定正则表达式。例如,假设您希望标记分隔符是逗号,后跟可选的空格。你会调用,

scanner.useDelimiter(",\s*");

Read more from http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

http://docs.oracle.com/javase/tutorial/essential/io/scanning.html阅读更多信息

回答by Ruchira Gayan Ranaweera

This will work for you

这对你有用

Scanner scanner = new Scanner(new File("D:\sample.txt"));
    while (scanner.hasNextLine()) {
        String token = scanner.nextLine();
        System.out.println(token);

    }