有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java比较两个文本文件,并计算第二个文件中的每个数字在第一个文件中出现的次数

编辑

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

字符串包含以下内容以供参考:

字符串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

字符串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();
}

}

第一个文件包含一组文本和数字,这就是为什么我必须使用matcher删除逗号和其他文本。我的问题是如何匹配另一个文本文件,并显示第二个文件中的数字在第一个文件中出现的次数。我使用match类还是使用。匹配nextLine或类似的东西。另外,代码放在哪里最好,我不想把已经存在的循环搞砸


共 (1) 个答案

  1. # 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
    

    由于第一个文件中有数字,所以无论何时从第二个文件中获得数字,都可以进行搜索