Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 12 out of bounds for length 12 at voteR.VoteR.main(VoteR.java:43)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading