扫描器类跳过空格 [英] Scanner class skips over whitespace

查看:41
本文介绍了扫描器类跳过空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用嵌套的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆