【问题标题】:Comparing two text files and calculating how many times each number in the 2nd file apears in the first比较两个文本文件并计算第二个文件中每个数字在第一个文件中出现的次数
【发布时间】:2016-09-27 23:44:20
【问题描述】:

编辑

这是更新后的代码,我得到了要加载匹配项的字符串,但现在如何将字符串与第二个文件匹配。

字符串包含此供参考:

字符串 1

1913 2016 1 1913 186 2016 1711 32843 2016 518 3 1913 32843 32001 4 250 5 3500 6 7 8 27 73 9 10 1711 73 11 2 12 1913 19 1930 20 21 1947 22 1955 23 1961 23 1969 27 1995 26 27 1962 28 29 30 1970 31 31

字符串 2

1.4 33.75278 84.38611

我需要看看每个数字与这个字符串匹配多少次

1913 2016 年 32843 31 27 1.4 4 7 2 23

public static void main(String[] args) throws FileNotFoundException {

    Scanner INPUT_TEXT = new Scanner(new File("C:\\Users\\josep\\OneDrive\\Classes\\Programming\\assignment3_1.txt"));
    INPUT_TEXT.useDelimiter(" ");
    int count = 0;
    ArrayList<String> integerInFirstFile =new ArrayList<>();
    ArrayList<String> decimalsInFirstFile =new ArrayList<>();

    while (INPUT_TEXT.hasNext()) {

        String TempString = INPUT_TEXT.next();
        String temp1 = TempString.replaceAll("[\\,]", "");
        String pattern1 = "[0-9]+\\.{1}[0-9]+";
        Pattern r1 = Pattern.compile(pattern1);
        Matcher m1 = r1.matcher(temp1);
        String pattern2 = "[0-9]+";
        Pattern r2 = Pattern.compile(pattern2);
        Matcher m2 = r2.matcher(temp1);

        if (!(m1.find())) {

            while (m2.find()) {

                count++;
                String number = m2.group(0);
                integerInFirstFile.add(number);

                if (count % 10 == 0) {
                    System.out.println(number);
                } else
                    System.out.print(number + " ");
            }
        } else {
            count++;
            String number = m1.group(0);
            decimalsInFirstFile.add(number);
            System.out.println("Next File");
            if (count % 10 == 0) {
                System.out.println(number);
            } else
                System.out.print(number + " ");

        }
    }

    Scanner INPUT_TEXT2 = new Scanner(new File("C:\\Users\\josep\\OneDrive\\Classes\\Programming\\assignment3_2.txt"));
    INPUT_TEXT2.useDelimiter(" ");

    System.out.println("");
    System.out.println("Next File");

    while (INPUT_TEXT2.hasNext()) {

        String TempString1 = INPUT_TEXT2.next();
        if (decimalsInFirstFile.equals(TempString1) || integerInFirstFile.equals(TempString1)) ;
        {
            System.out.println(TempString1);
        }
    }
    INPUT_TEXT.close();
    INPUT_TEXT2.close();
}

}

第一个文件包含一堆文本和数字,这就是我必须使用匹配器删除逗号和其他文本的原因。我的问题是如何匹配另一个文本文件并显示第二个文件中的数字出现在第一个文件中的次数。我是使用匹配类还是使用 .match nextLine 或类似的东西。另外,在哪里放置代码的最佳位置,我不想弄乱已经存在的循环。

【问题讨论】:

    标签: java text compare java.util.scanner matcher


    【解决方案1】:

    一旦您获得了一个号码,请将其存储起来以备将来使用,而不仅仅是显示它。

    Scanner INPUT_TEXT = new Scanner(new File("C:\\Users\\josep\\Downloads\\assignment3_1.txt"));
    INPUT_TEXT.useDelimiter(" ");
    int count=0;
    
    // ** Creating the storage for the numbers (as strings)
    ArrayList<String> numbersInFirstFile=new ArrayList<>();
    

    然后当你找到一个,存储它:

    while(INPUT_TEXT.hasNext()){
    
        String TempString=INPUT_TEXT.next();
        String temp1 = TempString.replaceAll("[\\,]", "");
        // other things that are needed where here, put them back
    
        if(!(m1.find())){
    
            while(m2.find( )) {
    
                count++;
                String number = m2.group(0);
    
                // *********************************
                // Hey, I found one, let's store it:
                numbersInFirstFile.add(number);
    
          // Do the same in other places where you find numbers in the first file
    

    由于您现在在第一个文件中获得了数字,因此每当您从第二个文件中获得数字时,您就可以搜索一些内容。

    【讨论】:

    • 非常感谢,这使得它现在变得如此容易,比较拖曳文件是否可以使用类似 TempString1.charAt(x-y).equals(intergerInFirstFIle))
    猜你喜欢
    • 2021-12-09
    • 2015-12-25
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多