how to change the layout of an input file and write to an output file
Hi
I have a .csv file which has a layout as schoolNo. , county1,county2,county3,county4,county5
It will need to go into an output file as schoolNo. repeated and a county on each record .
ie., schoolNo.,county1
schoolNo.,county2
schoolNo.,county3
schoolNo.,county4
schoolNo.,county5
I wrote the java program as follows ..which results in this error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
BECAUSE :--
i know bec., the first record doesnot have county3,county4,county5 . So when the if loop for county3 lenght is checked it gives this exception .
Pls help me write this program ..thanks in advance .
Code:
/Here I open the input file and read in record by record
BufferedReader readin;
try {
readin = new BufferedReader(new FileReader(InFile));
String firstLine = readin.readLine();
String[] headers = firstLine.split(",");
columnCount = headers.length;
System.out.println("Columns in ZIP Master File: "+columnCount);
for(String input ;(input = readin.readLine()) != null; ){
recCount++;
input = input.substring(1);
String[] column = input.split(",");
if(columnCount == 10){
eachIP++;
// OUTPUT FILE
if(column[1].length()> 0) {
String mainStr = column[0] + "," + column[1] ;
Count++;
totalcolumns++;
writeOutFile.write(mainStr);
writeOutFile.newLine();
}
else{
bypassedCount++;
}
if(column[2].length()>0){
String mainStr = column[0]+ "," + column[2];
Count++;
totalcolumns++;
writeOutFile.write(mainStr);
writeOutFile.newLine();
}
else{
bypassedCount++;
}
if(column[3].length()>0){
String mainStr = column[0]+ "," + column[3];
Count++;
totalcolumns++;
writeOutFile.write(mainStr);
writeOutFile.newLine();
}
else{
bypassedCount++;
}