[英]String.contains function not working
我必須檢查File1中的單詞是否存在於File2中,然后計數。 這兩個文件中的數據如下所示。
File1中的單詞如下所示:
File2中的數據如下所示:
我寫的代碼如下:
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;
}
問題在於,它對所有單詞返回0,而File1中的第一個單詞存在於File2的第一行。 如果我手動做這樣的事情
if(word.contains("發表")){
System.out.println("Success");
count++;
}
它會打印成功,否則不會嗎? 為什么會這樣呢?
問題在您的邏輯之內,因為您遍歷list
每個單詞,但是“ File2”上的掃描程序僅在此list
-loop外創建一次 。
您可能應該將列表循環移至if (word.contains(toMatch))
。
在您發表評論后,我對以下內容進行了快速測試:
package so36862093;
import com.google.common.io.Resources;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.util.*;
public class App {
public static void main(final String[] args) throws Exception {
final File file1 = new File(Resources.getResource("so36862093/ChineseWord.txt").toURI());
final List<String> list = Files.readAllLines(file1.toPath());
final File file2 = new File(Resources.getResource("so36862093/RandomData.txt").toURI());
final Scanner newScanner = new Scanner(new FileInputStream(file2));
final Map<String, Integer> count = new HashMap<>();
while(newScanner.hasNext()){
final String word = newScanner.nextLine();
for (String toMatch : list) {
if(word.contains(toMatch)){
System.out.println("Success");
count.put(toMatch, count.getOrDefault(toMatch, 0) + 1);
}
}
}
for (Map.Entry<String, Integer> e : count.entrySet()) {
System.out.println(e.getKey() + " exists " + e.getValue() + " times.");
}
}
}
以及ChineseText.txt
(UTF-8)
發表
發愁
發達
發抖
發揮
並在RandomData.txt
(UTF-8)中:
輸出是
Success
發表 exists 1 times.
后續:我對您共享的項目進行了一些操作,問題是在每一行的開頭您都有一個不間斷的空間U + 65279 (我沒有)。
因此,您可能應該先“剝離”該字符。
現在,您正在讀取整個文件,然后將其與列表中的第1個元素進行比較(反之亦然),請從file2中讀取第1行並將其與整個列表進行比較。
將您的代碼更改為->
while(newScanner.hasNext()){
String word = newScanner.nextLine();
for (int i = 0; i < list.size(); i++) {
String toMatch = list.get(i);
if(word.contains(toMatch)){
System.out.println("Success");
count++;
}
}
}
我認為您的問題出在編碼中:
Scanner newScanner = new Scanner(new FileInputStream(file2),"UNICODE");
試試看:
File file1 = new File("data/ChineseWord.txt");
Scanner sc = new Scanner(new FileInputStream(file1),"UNICODE");
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("data/RandomData.txt");
Scanner newScanner = new Scanner(new FileInputStream(file2),"UNICODE");
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.