-1
public static void main(String[] args) {
    BufferedReader bf = null;
    BufferedWriter bw = null;
    
    try {
        FileReader fr = new FileReader("File.txt");
        FileWriter fw = new FileWriter("Output.txt");
        bf = new BufferedReader(fr);
        bw = new BufferedWriter(fw);
        //Skip the first line
        bf.readLine(); 
        String line;
        List<String> finalCheck = new ArrayList<String>();
        //Read the file line by line and add the first number of each line (as a String) to the finalCheck List
        while((line = bf.readLine()) != null){
            String [] idArray = line.split(",");
            String idCheck = idArray[0];
            finalCheck.add(idCheck);
            
        }
        
        //Checking for any duplicates within the ID's and displaying them on
        int i = 0;
        int j = 0;
        for (i = 0; i <= finalCheck.size(); i++) {
            for (j = i + 1; j <= finalCheck.size(); j++) {
                String [] array = new String[finalCheck.size()];
                finalCheck.toArray(array);
                if(array[i].equals(array[j])) { //This is Line 43
                    fw.write("Duplicate at line: " + array[i]);
                }
            }
        }
        
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.err.println("File not found");
    } catch (IOException e) {
        System.err.println("Cannot read the file");
    } finally {
        try {
            bf.close();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}

}

I keep getting the error message shown in the title, and I am not sure how to fix it. Any idea's on what is going wrong to cause the issue? I added a note in the code to show exactly where Line 43 is, but if any additional information is needed please let me know and I will be more than happy to add it.

1 Answers1

0

j <= finalCheck.size() should be j < finalCheck.size().

For an array of size x, In Java, the last index is x - 1 because arrays in Java start at index 0.

hfontanez
  • 5,774
  • 2
  • 25
  • 37