扫描器类跳过空格 [英] Scanner class skips over whitespace
问题描述
我使用嵌套的 Scanner
循环从字符串行(从文本文件)中提取数字,如下所示:
String str = testString;扫描仪scanner = new Scanner(str);而 (scanner.hasNext()) {字符串令牌 = 扫描仪.next();//这里将使用每个令牌}
问题是这段代码会跳过所有空格" "
,但我也需要使用那些空格".那么 Scanner
可以返回空格还是我需要使用其他东西?
我的文本文件可能包含如下内容:
<预><代码>0011美国广播公司d2dsdwq斯达那些空行每行包含 1 个 " "
,而那些 " "
是我需要返回的.
使用 Scanner 的 hasNextLine()
和 nextLine()
方法,你会找到你的解决方案,因为这将允许您捕获空行或空白行.
I am using a nested Scanner
loop to extract the digit from a string line (from a text file) as follows:
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 Scanner
return the spaces or I need to use something else?
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.
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.
这篇关于扫描器类跳过空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!