ArrayIndexOutOfBoundsException issue (Beginner?)
Having some issues doing some coding work for a java class of mine. The program is supposed to read an file, split the lines into segments, replaces the lines if blank/empty and then reassemble the line.
The info in the file looks something like:
Jeffrey,Brandon,brand1jd,957030
Kayla,Neff,neff1km,736165
Michael,Wilson,wilso2mw,875541
The main issue I've been trying to wrap my head around is the fact that one of the lines in the file looks something like this:
Test,student,seeli1p_s,
Which the coding i have set up should recognize lacks a 4th segment and should place a "0" as the missing value in the array, yet it currently refuses to pick up that last value for the array. So I continually get a "ArrayIndexOutOfBoundsException" on that line and only that line of input.
Any ideas/hints as to how to fix/recode this so that it will recognize that the line missing a number at the end needs a "0" there.
Code Included Below:
public static String[] CSVReader (String[] lines)
{
for (int i = 0; i < 19 ; i++)
{
String[] lineitems = new String [4];
String[] altitems = new String [4];
lineitems = lines[i].split(",");
if (lineitems.length > 4)
{
System.out.println("Invalid line:" + lines[i]);
lines[i] = null;
}
else if (lineitems.length < 4)
{
if (lineitems[0].isEmpty())
lineitems[0] = "Missing First";
else if (lineitems[1].isEmpty())
lineitems[1] = "Missing Last";
else if (lineitems[2].isEmpty())
lineitems[2] = "Missing ID";
else
lineitems[3] = "0";
}
else
lineitems[2] = lineitems[2].toLowerCase();
lineitems[2] = lineitems[2].toLowerCase();
System.out.println(lineitems[0] + "," + lineitems[1] + "," + lineitems[2] + "," + lineitems[3]);
lines[i] = lineitems[0] + "," + lineitems[1] + "," + lineitems[2] + "," + lineitems[3];
}
return lines;
}
Re: ArrayIndexOutOfBoundsException issue (Beginner?)
Truthfully, there is no good way of doing this; just a couple of different hacks to get it working. Easiest will just be to open the text file and correct the record. You could write a function that goes through the text file and corrects it, but that can be a pain. Another option is to catch those ArrayOutOfIndexExceptions, notify the user there is a problem and continue without that record. If you are writing the records to the text file as part of a basic persistency exercise you should first check for blank data and 'pad' it with zero's or whitespaces so the records are always the same structure.
This problem you are having illustrates (one of) the shortcomings of text files as a medium for programmatically storing data. Whenever I have a project that requires reading/writing data from a text file I always start with the assumption that the file will always be correctly formatted. If my lecturer/client disagrees with my assumption I use XML. XML is the new industry standard for this kind of problem; it just makes more sense.
Re: ArrayIndexOutOfBoundsException issue (Beginner?)
Quote:
I continually get a "ArrayIndexOutOfBoundsException"
Always test the length of the array before trying to index it.