Search code examples
javatextcomparejava.util.scannermatcher

Comparing two text files and calculating how many times each number in the 2nd file apears in the first


Edit

Here is the updated code, I got the string to be loaded up with the matches but now how do I match the strings with the second file.

The strings contain this for reference:

String 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

String 2

1.4 33.75278 84.38611

And I need to see how many times each number matches with this string

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

}

The first file contains a bunch of text and numbers that why I had to use the matcher to remove the commas and other text. My question is how do I then match to the other text file and display how many times a number in the 2nd file appears in the first file. Do I use the match class or do I use .match nextLine or something similar. Also where is the best place to put in the code, I don't want to mess up the loops already in place.


Solution

  • Once you got one number, store it for a future use, don't just display it.

    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<>();
    

    Then when you find one, store it:

    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
    

    As you now have the numbers in the first file, whenever you get a number from the second file, you have something to search into.