如何在 Java 中使用 BufferedReader 计算文件中单词的出现次数
How to count number occurrences of a word in a file using BufferedReader in Java
任务是查找文件中特定单词的出现次数
那个人写给自己。
public void reader() {
BufferedReader myR = myReader("enter the name of a file: ");
int count = 0;
String substring = readLine("enter the string to count for entry: ");
try {
while (true) {
String s = null;
s = myR.readLine();
if (s == null)
break;
for(int j=0; j<s.length(); j++){
if(s.contains(substring)){
count++;
}
}
}
myR.close();
} catch (IOException e) {
throw new ErrorException(e);
}
println("number of words is: " + count);
}
private BufferedReader myReader(String prompt) {
BufferedReader rd = null;
while (rd == null) {
try {
String name = readLine(prompt);
rd = new BufferedReader(new FileReader(name));
} catch (FileNotFoundException e) {
println("wrong file entered");
// e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return rd;
}
所以问题是,如果在我的文本文件中我检查的字数是 4,但代码打印 671
,我不知道该怎么办
问题出在这个循环中:
while (true) {
String s = null;
s = myR.readLine();
if (s == null)
break;
for(int j=0; j<s.length(); j++){
if(s.contains(substring)){
count++;
}
}
}
现在假设您的 bufferedReader 读取一行 "hie i am user"。
这个字符串的大小是 13 所以 string.length();会 return 13.
这意味着您将为您的匹配检查同一行 13 次迭代。
因此,假设如果您要查找匹配项,请说 "user",然后在同一行上检查 "user" 13 次将使您的计数达到 13。
你可以用这段代码替换上面的代码:
while (true) {
String s = null;
s = myR.readLine();
if (s == null)
break;
String[] slist = s.split(" ");
for(int j=0; j<slist.length(); j++){
if(slist[j].contains(substring)){
count++;
}
}
}
哦哦!!你应该提到你想在不使用数组的情况下做到这一点。
这段代码应该对您有所帮助:
while (true) {
String s = null;
s = myR.readLine();
if (s == null)
break;
for(int j=0; j<s.length(); j++){
if(s.equals(" ")){
String temp = s.substring(j+1, s.length());
String word = temp.substring(0,temp.indexOf(" ")-1);
if(temp.equalsIgnoringCases(word)){
count++;
}
}
}
}
现在我在这里做的是首先我在整个字符串中寻找 space,找到一个后,我从 [= 的索引旁边的索引开始提取一个子字符串19=] 到字符串的末尾。
现在从这个提取的子字符串中,我进一步提取从索引零到第一个 space 的子字符串。这个字符串本质上是一个适合比较的词
任务是查找文件中特定单词的出现次数 那个人写给自己。
public void reader() {
BufferedReader myR = myReader("enter the name of a file: ");
int count = 0;
String substring = readLine("enter the string to count for entry: ");
try {
while (true) {
String s = null;
s = myR.readLine();
if (s == null)
break;
for(int j=0; j<s.length(); j++){
if(s.contains(substring)){
count++;
}
}
}
myR.close();
} catch (IOException e) {
throw new ErrorException(e);
}
println("number of words is: " + count);
}
private BufferedReader myReader(String prompt) {
BufferedReader rd = null;
while (rd == null) {
try {
String name = readLine(prompt);
rd = new BufferedReader(new FileReader(name));
} catch (FileNotFoundException e) {
println("wrong file entered");
// e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return rd;
}
所以问题是,如果在我的文本文件中我检查的字数是 4,但代码打印 671
,我不知道该怎么办问题出在这个循环中:
while (true) {
String s = null;
s = myR.readLine();
if (s == null)
break;
for(int j=0; j<s.length(); j++){
if(s.contains(substring)){
count++;
}
}
}
现在假设您的 bufferedReader 读取一行 "hie i am user"。
这个字符串的大小是 13 所以 string.length();会 return 13.
这意味着您将为您的匹配检查同一行 13 次迭代。
因此,假设如果您要查找匹配项,请说 "user",然后在同一行上检查 "user" 13 次将使您的计数达到 13。
你可以用这段代码替换上面的代码:
while (true) {
String s = null;
s = myR.readLine();
if (s == null)
break;
String[] slist = s.split(" ");
for(int j=0; j<slist.length(); j++){
if(slist[j].contains(substring)){
count++;
}
}
}
哦哦!!你应该提到你想在不使用数组的情况下做到这一点。
这段代码应该对您有所帮助:
while (true) {
String s = null;
s = myR.readLine();
if (s == null)
break;
for(int j=0; j<s.length(); j++){
if(s.equals(" ")){
String temp = s.substring(j+1, s.length());
String word = temp.substring(0,temp.indexOf(" ")-1);
if(temp.equalsIgnoringCases(word)){
count++;
}
}
}
}
现在我在这里做的是首先我在整个字符串中寻找 space,找到一个后,我从 [= 的索引旁边的索引开始提取一个子字符串19=] 到字符串的末尾。
现在从这个提取的子字符串中,我进一步提取从索引零到第一个 space 的子字符串。这个字符串本质上是一个适合比较的词