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;
}