Java 计算字符串在文件中出现的次数
我试图找出如何读取文件,然后计算某个字符串出现的次数 这是我的文件的外观,它是一个.txt文件:Java 计算字符串在文件中出现的次数,java,string,filereader,Java,String,Filereader,我试图找出如何读取文件,然后计算某个字符串出现的次数 这是我的文件的外观,它是一个.txt文件: Test Test Test Test 我希望该方法返回它在文件中的次数。你知道我该怎么做吗?我主要需要第一部分的帮助。因此,如果我搜索字符串“Test”,我希望它返回4 提前感谢!希望我提供了足够的信息 将此方法添加到类中,将FileInputStream传递给它,它将返回文件中的字数。请记住,这是区分大小写的 public int countWord(Strin
Test
Test
Test
Test
我希望该方法返回它在文件中的次数。你知道我该怎么做吗?我主要需要第一部分的帮助。因此,如果我搜索字符串“Test”,我希望它返回4
提前感谢!希望我提供了足够的信息 将此方法添加到类中,将FileInputStream传递给它,它将返回文件中的字数。请记住,这是区分大小写的
public int countWord(String word, FileInputStream fis) {
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String readLine = "";
int count = 0;
while((readLine = in.readLine()) != null) {
String words = readLine.split(" ");
for(String s : words) {
if(s.equals(word)) count++;
}
return count;
}
现在刚写的,还没有测试过,所以让我知道它是否有效。此外,如果这是一个家庭作业问题,请确保您理解我所做的。给您:
public int countStringInFile(String stringToLookFor, String fileName){
int count = 0;
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
int startIndex = strLine.indexOf(stringToLookFor);
while (startIndex != -1) {
count++;
startIndex = base.indexOf(stringToLookFor,
startIndex +stringToLookFor.length());
}
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return count;
}
用法:
int count=countStringInFile(“SomeWordToLookFor”、“FileName”)代码>如果要将每个文件读入字符串,我建议查看string方法split
给它字符串代码“Test”,它将返回一个字符串类型的数组-计算每行的元素数。把它们加起来,得到你的总发生率
import java.io.*;
public class StringCount {
public static void main(String args[]) throws Exception{
String testString = "Test";
String filePath = "Test.txt";
String strLine;
int numRead=0;
try {
FileInputStream fstream = new FileInputStream(filePath);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((strLine = br.readLine()) != null) {
strLine = strLine + " ";
String [] strArry = strLine.split(testString);
if (strArry.length > 1) {
numRead = numRead + strArry.length - 1;
}
else {
if (strLine == testString) {
numRead++;
}
}
}
in.close();
System.out.println(testString + " was found " + numRead + " times.");
}catch (Exception e){
}
}
}
我会这样做:
逐行打开并读取文件
检查一行包含给定单词的频率
为此增加一个全局计数器。。
到目前为止你做了什么?这看起来像是家庭作业,我所做的只是让我的程序获取一个字符串(玩家的名字),每次都将其写入一个新行上的文件中。这不是家庭作业,这是我的游戏的警告/禁止系统。假设字符串每行只出现一次。非常好,谢谢。“我对所有这些文件的东西还是很陌生的。”zomby很高兴它奏效了。请查看我的更新答案,以计算一行中出现多次的单词感谢代码,碰巧我每行只写了一个单词,所以它工作正常startIndex=base.indexOf(stringToLookFor,startIndex+stringToLookFor.length())
这里的字符串base
是什么?每次只返回1。“测试”写了6次。知道为什么吗?你能提供你的测试文件吗?我针对它运行的所有测试都返回了正确的结果,所以我想知道我错过了什么用例;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("Test.txt"));
Scanner sc = new Scanner(System.in);
System.out.print("Enter the subtring to look for: ");
String word = sc.next();
String line = in.readLine();
int count = 0;
do {
count += (line.length() - line.replace(word, "").length()) / word.length();
line = in.readLine();
} while (line != null);
System.out.print("There are " + count + " occurrences of " + word + " in ");
}