比较2个字符串数组android

时间:2016-05-11 09:59:37

标签: java android string file

我想比较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++;
            }
        }   

1 个答案:

答案 0 :(得分:0)

您可以使用此代码。

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))
{
   ....
}

当行为空时,您也可以输入文件。