Scanner类跳过空白 [英] Scanner class skips over whitespace

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

问题描述

我使用的是嵌套的扫描循环提取一个串线的数字(从文本文件)如下:

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
}

问题是这样的code将跳过所有空格,但我还需要使用这些空间了。这样可以扫描返回空格或我需要用别的东西吗?

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

这些空行包含1 每一个,而那些是我需要的东西回来了。

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

推荐答案

使用扫描仪的 hasNextLine() nextLine()方法,你会发现你的解决方案,因为这将让你捕捉空或空白行。

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.

这篇关于Scanner类跳过空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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