gpt4 book ai didi

java - 在扫描仪中检查带有空格的子字符串

转载 作者:行者123 更新时间:2023-12-01 18:32:18 24 4
gpt4 key购买 nike

我正在读取一个多行文件,该文件的内容示例是:

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);

我的问题是我的程序的 int 迭代次数为 0。

最佳答案

What I am trying to do is to get my program to read the number of lines that contains LOOP with spaces

尝试这样。

if (scanner.nextLine().contains("LOOP with spaces"))
{ iterated++; }
  • nextLine()

此方法将读取整行。

String line=scanner.nextLine();
  • 下一个()

这将返回文件中的下一个标记next() 只能读取输入直到空格。

String token=scanner.next();

关于java - 在扫描仪中检查带有空格的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23631930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com