Results 1 to 9 of 9
- 05-12-2010, 05:04 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 119
- Rep Power
- 0
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 .
Java 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++; }
Last edited by renu; 05-12-2010 at 05:26 PM.
- 05-12-2010, 05:24 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
Check the length of the column array using an if before accessing an element in the array.
- 05-12-2010, 05:37 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Crossposted.
kind regards,
Jos
- 05-12-2010, 05:38 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 119
- Rep Power
- 0
Hi
I didnot understand what you want me to do in the code i wrote.
can you pls help me , change my code .
thank you
- 05-12-2010, 05:41 PM #5
Senior Member
- Join Date
- May 2010
- Posts
- 119
- Rep Power
- 0
- 05-12-2010, 06:51 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Don't think from yourself, think about people who may spend their time answering your question only to find out that your question has also been answered on another website before; those people have wasted their time and effort. It is just courtesy to mention the fact that you have posted your question somewhere else so people can check out both threads.
kind regards,
Jos
- 05-12-2010, 07:22 PM #7
Senior Member
- Join Date
- May 2010
- Posts
- 119
- Rep Power
- 0
Hi
The input files record looks like this
1220,10,20,,,,
Once the code comes to
if(column[3].length()>0){
String mainStr = column[0]+ "," + column[3];
Count++;
totalcolumns++;
writeOutFile.write(mainStr);
writeOutFile.newLine();
}
else{
bypassedCount++;
}
--------------------------------------------------------------------------------
Last edited by renu; Today at 03:26 PM.
i get array out of bound exception..pls help
why doesnot the array element atleast pick up null in it
- 05-12-2010, 07:38 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-12-2010, 08:19 PM #9
Senior Member
- Join Date
- May 2010
- Posts
- 119
- Rep Power
- 0
Similar Threads
-
How do I write to an existing file in Java with a program that asks for user input?
By gmoney8316 in forum New To JavaReplies: 13Last Post: 04-16-2010, 03:51 AM -
Write to a txt. file, via user input? (java with netbeans)
By Glypsen in forum NetBeansReplies: 6Last Post: 03-01-2010, 08:00 AM -
reading from input file and then write on it
By sara12345 in forum New To JavaReplies: 9Last Post: 01-19-2010, 12:41 PM -
Search a word(taken from one file) in another file and give the line as the output
By SwapnaNaidu in forum New To JavaReplies: 7Last Post: 11-19-2008, 03:09 PM -
how to write the output of the console to a file
By fred in forum New To JavaReplies: 1Last Post: 07-24-2007, 03:02 AM
Bookmarks