我想計數匹配和計數單詞。錯誤在輸出的單詞計數在java
我有兩個文本文件,並比較相互文字。
例如,
一個文本文件:a b c d e。
b文本文件:a a a a a。
我想看到這個輸出。
輸出:a 5.
但是當我寫代碼時,它沒有工作。
請幫幫我。
我寫的代碼爲java adk 1.8使用日食,Windows 8.1 64位。
這是下面的代碼。
package test1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ex01 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileReader fr = new FileReader("C:/Users/Hong/Desktop/승현연구/152-300/301.txt");
FileReader key_item = new FileReader("C:/Users/Hong/Desktop/승현연구/no-yes2500.txt");
BufferedReader br = new BufferedReader(fr);
BufferedReader br2 = new BufferedReader(key_item);
FileOutputStream file = new FileOutputStream("C:/Users/Hong/Desktop/승현연구/답변빈도/a301.txt");
List<String> key = new ArrayList<String>();
List<String> str = new ArrayList<String>();
String in = "";
String s = "";
String ss[];
while ((in = br2.readLine()) != null) {
key.add(in);
}
while ((s = br.readLine()) != null) {
str.add(s);
}
***int cnt = 0;
int count = 0;
int cont = 0;
String txt = "";
for (int i = 0; i < key.size(); i++) {
for (int j = 0; j < str.size(); j++) {
System.out.println(j + " " + str.get(j));
if (str.get(j).lastindexOf(key.get(i))) {
cnt++;
//System.out.println(key.get(i) + " " + cnt);
}
if (cnt == 1){
//cont ++ 1;
//System.out.printf("%d",cont);
}
}
System.out.println(key.get(i) + " " + cnt);
txt = txt + key.get(i) + " " + cnt + "\n";
cnt = 0;
}
file.write(txt.getBytes());
}***
//System.out.println("Hello Java");
}
在我的編碼
,錯誤causeing此行 [IF(str.get(J).lastindexOf(key.get(I)))] 我不知道爲什麼 這是摘要說明文本文件和我想要做的事
首先,我想看到的代碼是比較301文本文件和no-yes2500文本文件並輸出屬於no-yes2500的字數 (例如:apple 3 香蕉2)
- 301.txt是一個由關於Q &的句子組成的文本文件。
- 沒有yes2500.txt是一個關鍵詞列表
所以,我想我明白你的程序,但你有什麼問題嗎? – Minh
aaaa我的問題是這樣的。 這段代碼會導致if(str.get(j).lastindexOf(key.get(i)))這行的錯誤。並且,我想修復此代碼。並想知道爲什麼會出現這個錯誤 –
那麼'a 5'是什麼? b文本文件中的** a **總數是多少?這只是一個例子而已.ShreeKrishna @ –