I have to check whether the words from File1 exist in File2 or not and then count. Data in both files is shown below.
The words in File1 are like shown below:
- 发表
- 发愁
- 发达
- 发抖
- 发挥
The data in File2 is like shown below:
- 这篇论文是什么时候发表的?
- 91 .数据 删掉 被马工程师 了
- 92 .驾驶 酒后 很大 危害
- 93 .客观地 要 他人 评价
- 94 .我不小心 水壶 打翻 了 把
The code I have written is as follows:
File file1 = new File("ChineseWord.txt");
Scanner sc = new Scanner(new FileInputStream(file1));
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> newList = new ArrayList<String>();
while(sc.hasNext()){
list.add(sc.next());
}
sc.close();
File file2 = new File("RandomData.txt");
Scanner newScanner = new Scanner(new FileInputStream(file2));
int count = 0;
for (int i = 0; i < list.size(); i++) {
while(newScanner.hasNext()){
String word = newScanner.nextLine();
String toMatch = list.get(i);
if(word.contains(toMatch)){
System.out.println("Success");
count++;
}
}
String test = list.get(i);
newList.add(test+"exists" + count+ "times");
count =0;
}
The question is that it returns 0 for all words whereas the very first word in File1 exists in the very first line of File2. If I manually do something like this
if(word.contains("发表")){
System.out.println("Success");
count++;
}
It prints successs otherwise it does not ? Why is this so?