A problem

Suppose we wanted to write a program that would compare two lists of integers to see if they contain the same numbers. The numbers in the two lists can appear in any order, and numbers can be repeated. For example, the lists

2 4 8 3 9 4

and

9 4 2 8 4 3

contain the same numbers.

This lab exercise will take you through the process of writing a program that will solve this problem.

(Note that this problem gets easier to solve if we sort both the lists. Since the purpose of this lab exercise is mostly to get some practice working with arrays and methods, we will not take this approach.)

Getting started

For convenience, we should assume that the numbers in question are stored in two separate text files.

The first thing we will need is some code to read a list of integers from a text file and place them in an array. The following method does this.

public static int[] readNumbers(String fileName) {
  Scanner input = null;
  try {
    input = new Scanner(new File(fileName));
  } catch(Exception ex) {
    ex.printStackTrace();
    System.exit(0);
  }

  int N = 0; // Counts how many numbers are in the file.
  while(input.hasNextInt()) {
    int unused = input.nextInt();
    N++;
  }
  input.close();

  int A[] = new int[N]; // Make an array with the right size

  try {
    input = new Scanner(new File(fileName));
  } catch(Exception ex) {
    ex.printStackTrace();
    System.exit(0);
  }

  for(int n = 0;n < A.length;n++)
    A[n] = input.nextInt();
  input.close();

  return A;
}

Write a second method

public static void printNumbers(int A[])

that prints a list of numbers stored in an array to System.out, one number per line. This will allow you to confirm that you have read the numbers correctly.

Use these two methods to write a program that reads a list of numbers from a text file and then prints them to System.out.

A strategy to solve the original problem

Here is a strategy to solve the list comparison problem.

  1. Read both lists into arrays one and two.
  2. Check the lengths of the two arrays. If they differ, stop and report that the arrays are not the same.
  3. For each number in array one do this:
    1. Count how many times that number appears in array one.
    2. Count how many times that number appears in array two.
    3. If the counts differ, stop and report that the arrays are not the same.

To implement this strategy, write code for the following two methods:

public static int countOccurances(int x,int A[])
public static boolean compare(int one[],int two[])

The first method counts how many times x appears in the array A. The second method compares the contents of two arrays using the strategy outlined above, and returns true if the two arrays contain the same numbers and false if they do not.

Write a program that uses this strategy to compare the contents of two files of ints. Your program should open the two files, compare their contents, and print a message that says whether or not the two files contain the same numbers.