Java Array Filling with Only Last Record’s Data

Having spent hours trying to find the bug in my logic, I’ve finally found the offending item. Talk about obscure! Here’s the offending block of code (now working). What was wrong? The item shown in blod-italic. I had declared this as global by placing it at the top of my class (main module). Counter-intuitively, but making this global, it overwrote every record in linecolsarray (marked in purple).

Lesson learned here: Declare local variables unless you really need to make them global !

        while ((line = br.readLine()) != null) {

            // Convert line into columns
            String[] columns = line.split(DELIMITER);
            String[] linecols = new String[5];

            String leftchar = columns[3].substring(0,1);

                linecols[0] = columns[0];
                linecols[1] = columns[1];
                linecols[2] = columns[2];
                linecols[3] = columns[3];
                linecols[4] = "g"+columns[3].substring(1);

                linecolsarray[lineref] = linecols;                  

            lineref = lineref + 1;

Please follow and like my site: