[英]Counting the number of times a specific word appears in a txt file while using a try/catch block in java
[英]Counting the number of occurence of a word that appears between the same patterns in a txt file
我有一个 txt 文件,其中特定的相同行(模式,在本例中为 AAAAAAA)出现了一定次数。 我想计算每2个图案之间出现1的次数,即即使在两个图案之间出现多次,我也计算一次。 示例:我的文件如下所示。 返回的结果应该是 2,因为在每个 AAAAAAA 之间 1 出现了 2 次。 非常感谢您提前。
AAAAAAA
0
0
1
AAAAAAA
0
0
AAAAAAA
1
0
1
我会使用 Java FileReader 并解析每一行。 如果您找到了您要查找的字符串(在您的示例中为“AAAAAA”),您将开始一个新块,并在每个块中检查您是否找到至少一个 1。如果找到,则将计数器增加 1,然后返回检查所有行后的值。 如果您真的只想计算 1,如果它是两个“AAAAAA”之间的(在您的示例中,您还计算了最后一个),那么您需要在最后删除添加(请参阅我在代码中的注释)
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws FileNotFoundException {
//call the function that calculates the output and give it the path of the textfile to be used
System.out.println(getNrOfOnesBetweenString("enterPathToYourTextFileHere.txt"));
}
private static Integer getNrOfOnesBetweenString(String filePath) throws FileNotFoundException {
//store whether we found a 1 in this block
Boolean oneFoundInThisBlock = false ;
//this is the counter for the output we want to return
Integer numberOfBlocksWithOne = 0 ;
//this is the string between which we want to search a 1
String stringToSearch = "AAAAAAA";
//read the file
BufferedReader br = new BufferedReader(new FileReader(filePath));
try {
String line;
//foreach line
while ((line = br.readLine()) != null) {
//check if this line contains the string to search for
if(line.equals(stringToSearch)) {
//if yes: start a new block
if(oneFoundInThisBlock) {
//if there was a 1 since the last occurrence of stringToSearch, increase the counter
numberOfBlocksWithOne++;
}
//reset for next block
oneFoundInThisBlock = false;
}
//see if this line has a 1
if (line.contains("1")){
oneFoundInThisBlock = true;
}
}
// add one for the last block -> remove this if you only want to count 1 if they are BETWEEN two "AAAAAA" (in your example you also counted the last one after so not sure what you need)
if(oneFoundInThisBlock){
numberOfBlocksWithOne++;
}
} catch(IOException e){
System.out.println("Could not read from file "+filePath);
}
return numberOfBlocksWithOne;
}
}
这将为您提供以下值:
示例 1:
AAAAAAA
0
0
1
AAAAAAA
0
0
AAAAAAA
1
0
1
输出(示例 1):2
示例 2:
AAAAAAA
0
AAAAAAA
0
AAAAAAA
1
AAAAAAA
1
AAAAAAA
1
输出(示例 2):3
示例 3:
AAAAAAA
0
0
0
AAAAAAA
0
1
AAAAAAA
1
1
AAAAAAA
1
1
1
AAAAAAA
1
1
1
输出(示例 3):4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.