【问题标题】:compare 2 string array android比较2个字符串数组android
【发布时间】:2016-05-11 09:59:36
【问题描述】:

我正在尝试比较 2 个文本文件。我可以读取文件并将值分配到单词中。然后将此单词分配给数组。比较时,它不起作用。 numCommon= 1 的结果,但这是错误的。你能帮助我吗? File1 示例:1-A 2-B 3-C 4-D... File2 示例:1-A 2-B 3-E 4-A ..like 继续。

        sc = new Scanner(new FileInputStream(dir + "/" + sdf.format(file.lastModified()) + ".txt"));

        sc2 = new Scanner(new FileInputStream(dir2 + "/" + sdf.format(file2.lastModified()) + ".txt"));
        String dirPath = dir + "/" + sdf.format(file.lastModified()) + ".txt";
        String dir2Path =dir2 + "/" + sdf.format(file2.lastModified()) + ".txt";
        int ss= satirSayisi(dirPath);
        int ss2= satirSayisi(dir2Path);
        String[] wordListOne , wordListTwo;
        while (sc.hasNextLine()) {

            word1 = sc.nextLine();
            word1 = word1.replaceAll("\\s+","").trim();

            if (word1.length() != 0 ) {
                wordListOne = new String[ss];
                wordListOne[i] = word1;
                System.out.println("wordListOne: " + wordListOne[i]);
                while(sc2.hasNextLine()){
                       word2 = sc2.nextLine();
                       word2 = word2.replaceAll("\\s+","").trim();
                       if(word2.length() != 0){

                           wordListTwo = new String[ss2];
                           wordListTwo[j] = word2;
                           System.out.println("wordListTwo: " + wordListTwo[j]);

                           if(wordListOne[i].equals(wordListTwo[j])) numCommon++;System.out.println("count " + numCommon);
                        j++;
                       }
                }
            i++;
            }
        }   

【问题讨论】:

    标签: java android string file


    【解决方案1】:

    您可以使用此代码。

    Scanner input1= new Scanner(new FileInputStream(dir + "/" +   sdf.format(file.lastModified()) + ".txt"));
    Scanner input2= new Scanner(new FileInputStream(dir2 + "/" + sdf.format(file2.lastModified()) + ".txt"));
    
    String one= input1.nextLine();
    String two= input2.nextLine();
    
    if(one.equals(two))
    {
       ....
    }
    

    您也可以在行为空时上交文件。

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多