2016-04-26 304 views
1

我必须检查File1中的单词是否存在于File2中,然后进行计数。这两个文件中的数据如下所示。String.contains函数不起作用

File1中的字被如下所示:

  1. 发表
  2. 发愁
  3. 发达
  4. 发抖
  5. 发挥

在文件2的数据被如下所示:

  1. 这篇论文是什么时候发表的?
  2. 91。数据删掉被马工程师了
  3. 92。驾驶酒后很大危害
  4. 93。客观地要他人评价
  5. 94 。我不小心水壶打翻了把

我写的代码如下:

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++; 
        } 

它打印成功,否则它不会?这是为什么?

+0

见http://stackoverflow.com/questions/22048692/check-if-string-contains-cjk-chinese-characters and http://stackoverflow.com/questions/26357938/detect-chinese-character-in-java – Adi

+1

我会确保字符编码读取是您写的。您可以尝试使用UTF-8或UTF-16LE,但必须保持一致。 –

+0

字符编码是UTF-8 – indexOutOfBounds

回答

2

的问题是你的逻辑中,因为你循环遍历每个list话,但你的“文件2”的扫描仪只能创建一次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):

For some reason this text cannot be copy pasted

输出是


后续:我打一点与您共享的项目,问题是,你必须在每行的开始非打破空间U+65279(我做不)。

插图: debug session

所以,你应该"strip"那之前别的字符。

+0

是的,我明白这一点。 for循环在while循环中,我一直在做很多事情,所以这就是为什么我发布我的最后一个代码,它有点搞砸了。问题是,如果你尝试这样做,它不会让第一个词成功。为什么? – indexOutOfBounds

+0

固定代码适用于我,您应该仔细检查您的代码和输入。 – 2016-04-26 11:23:10

+0

我刚刚复制了你的代码。检查了所有的文本文件编码,但它不适合我?它有什么不对? – indexOutOfBounds

2

现在你正在读取整个文件,然后将它与列表中的第一个元素进行比较,它应该是相反的方向,从file2读取第一行并将其与整个列表进行比较。

更改您的代码 - >

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++; 
     } 
    } 
} 
+0

我完全明白这一点。我删除了文件1中除第一个以外的所有单词。if(word.contains(“发表”)){0} {0} \t count ++; \t}正在工作,而不是其他方式? – indexOutOfBounds

0

我觉得你的问题是在编码:

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; 

    } 
+0

不,它不起作用。控制台中的输出已被更改为一些奇怪的字符。 – indexOutOfBounds