Java Help

Associate
Joined
14 Sep 2005
Posts
1,555
Location
All over.
Code:
public class Fraud {

    public static final int ARRSIZE=10000;
    private String [] niNumbers = new String[ARRSIZE];
 
    public Fraud(String file) {
        
        String thisLine;
        
        for (int i = 0; i <= 10000; i++){
            
            try{
                
            BufferedReader br = new BufferedReader (new FileReader (file));
            while ((thisLine = br.readLine()) != null){
                
                niNumbers[i] = thisLine;
                i++;
                System.out.println(thisLine);
            }
            }//end try
            
            catch (IOException e){
                System.err.println("Error: Cannot open " +file +" + e");
                System.exit(-1);
            }//end catch    
        
    
        }//end of for loop    
    } // end of constructor
   
public static void main(String args[]) {
    
        Fraud notts = new Fraud("notts.txt");
        Fraud derbs = new Fraud("derbs.txt");
        Fraud leics = new Fraud("leics.txt");
        
        
        
        String[] duplicates=notts.findDuplicates(derbs);
I'm having a bit of trouble with a bit of coding. I'm trying to compare 3 text files containing a list of strings. I'm trying to find duplicates between these files. Can't work out how to compare the objects once the've been read in.

Can anyone help? Cheers.
 
Not the most elegant solution but it works.

It is a bad idea to use an array (static collection) when you have no idea how big the collection will be, you could count the lines on the file but it's far easier to use a dynamic collection such as a ArrayList.

Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Fraud {

	private ArrayList<String> niNumbers;

	public Fraud(String file) {
		niNumbers = new ArrayList<String>();
		readFile(file);
	}

	private void readFile(String file) {
		String thisLine;
		try{    
			BufferedReader br = new BufferedReader (new FileReader (file));
			while ((thisLine = br.readLine()) != null){
				niNumbers.add(thisLine);
				System.out.println(thisLine);
			}
		} catch (IOException e) {
			System.err.println("Error: Cannot open " +file +" "+ e);
			System.exit(-1);
		}
	}

	public ArrayList<String> getNiNumbers() {
		return niNumbers;
	}

	public String[] findDuplicates(Fraud fr) {
		ArrayList<String> data = fr.getNiNumbers();
		ArrayList<String> duplicates = new ArrayList<String>();
		for (String str : niNumbers) {
			if (data.contains(str)) {
				duplicates.add(str);
			}
		}
		String[] dupearr = new String[duplicates.size()];
		for (int i=0; i < duplicates.size(); i++) {
			dupearr[i] = duplicates.get(i);
		}
		return dupearr;
	}

	public static void main(String args[]) {

		Fraud notts = new Fraud("notts.txt");
		Fraud derbs = new Fraud("derbs.txt");
		Fraud leics = new Fraud("leics.txt");

		String[] duplicates = notts.findDuplicates(derbs);
	}
}
 
Back
Top Bottom