java - 比较两个字符串数组在不同的地方给出不同的输出

标签 java file arraylist

我有一个方法,可以正常工作并给出所需的结果。这是它的代码:

public class arraycomparison {

public static void main(String args[]) {
    try {

        Scanner sc1 = new Scanner(new FileInputStream("/Users/esna786/File1.txt"));
        Scanner sc2 = new Scanner(new FileInputStream("/Users/esna786/File2.txt"));
        List<String> File1Content = new ArrayList<String>();
        List<String> File2Content = new ArrayList<String>();
        List<String> commonWords = new ArrayList<String>();

        //Getting the contents of File1
        while (sc1.hasNext()) {
            File1Content.add(sc1.next());
        }
        String arr1[] = File1Content.toArray(new String[File1Content.size()]);

        //Getting the contents of File1
        while (sc2.hasNext()) {
            File2Content.add(sc2.next());
        }
        String arr2[] = File2Content.toArray(new String[File2Content.size()]);

        int count = 0;

        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i].equals(arr2[j])) {
                    commonWords.add(arr1[i]);
                    count++;
                }
            }
        }

        String[] comWords = commonWords.toArray(new String[commonWords.size()]);

        System.out.println("Total Number of Common Words are: "+comWords.length);
        for (int i = 0; i < comWords.length; i++) {
            System.out.println(i + 1 + ": " + comWords[i]);
        }


    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

它的输出是,

Total Number of Common Words are: 7
1: apple
2: ball
3: cat
4: duck
5: elephant
6: fan
7: goat
BUILD SUCCESSFUL (total time: 0 seconds)

当我运行下面的程序代码时使用相同的文件几乎与上面相同:

//Counting the number of matched words
public int getCommonWords(File File1, File File2) throws IOException {
    try {

        Scanner sc1 = new Scanner(new FileInputStream(File1));
        Scanner sc2 = new Scanner(new FileInputStream(File1));
        List<String> File1Content = new ArrayList<String>();
        List<String> File2Content = new ArrayList<String>();
        List<String> commonWords = new ArrayList<String>();

        //Getting the contents of File1
        while (sc1.hasNext()) {
            File1Content.add(sc1.next());
        }
        String arr1[] = File1Content.toArray(new String[File1Content.size()]);

        //Getting the contents of File1
        while (sc2.hasNext()) {
            File2Content.add(sc2.next());
        }
        String arr2[] = File2Content.toArray(new String[File2Content.size()]);

        int count = 0;

        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i].equals(arr2[j])) {
                    commonWords.add(arr1[i]);
                    count++;
                }
            }
        }

        String[] comWords = commonWords.toArray(new String[commonWords.size()]);

        System.out.println("Total Number of Common Words are: " + comWords.length);
        for (int i = 0; i < comWords.length; i++) {
            System.out.println(i + 1 + ": " + comWords[i]);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

它在输出区域显示以下内容:

run:
Total words of File1.txt are: 11
Total Number of Common Words are: 11
1: apple
2: ball
3: cat
4: duck
5: elephant
6: fan
7: goat
8: a
9: b
10: c
11: d

还显示不匹配的单词,为什么会这样呢? 请帮忙。

最佳答案

这是因为在您的第二个源中,您正在读取如下所示的同一文件:

Scanner sc1 = new Scanner(new FileInputStream(File1));
Scanner sc2 = new Scanner(new FileInputStream(File1));//change this to File2

关于java - 比较两个字符串数组在不同的地方给出不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27674786/

相关文章:

java - Android PdfDocument 在保存到外部存储时损坏

c++ - 存储在文件中的记录

c - 将TXT转换为BIN,并打印出BIN的内容

java - 检查用户输入的文件名/路径是否有效

java - Java中二维数组类型的ArrayList

java - 获取 ArrayList 中的元素后将其删除

java - 在 OpenShift JBoss AS 7 中更改 URIEncoding

java - 为什么没有写JSON?

java - 如何在java中获取对象的内存位置?

java - 将 JSON 解析为自定义 ArrayList,只返回最后一项?