【发布时间】:2014-05-13 12:55:54
【问题描述】:
我正在读取一个包含多行的文件,该文件的内容示例是:
Reader : INFO - LOOP with sapces : 1
Reader : INFO - LOOP with sapces : 2
Reader : INFO - LOOP with sapces : 17
Reader : INFO - LOOP with sapces : 201
我要做的是让我的程序读取包含带空格的 LOOP 的行数。
这是我的代码:
Scanner fileScan = new Scanner(file);
int iterated = 0;
while (fileScan.hasNext())
{
String somestring = fileScan.next();
if (somestring.matches(".*LOOP\\swith\\sspaces.*"))
{ iterated++; }
}
}
System.out.println("number of times found is: "+iterated);
我的问题是我的程序是 0 用于 int 迭代。
【问题讨论】:
-
我们能知道您为什么要使用
matches而不是contains吗?您搜索的子字符串LOOP with spaces可以有不同的形式还是固定的?
标签: java regex java.util.scanner