I'm running a simple java program. I programmed it in Netbeans. Everything worked great. Attempted to transition code to use Ant because that's what my class requires, and I'm getting a weird error.
All text coming in from the .txt is in the format,
Baker William Chavez 04/01/05
Sanchez Jose Chavez 06/15/05
etc ...
There about 20 entries, 3 names and a date. Each entry is on its own line.
I'm using this code to read it in.
while ((line = br.readLine()) != null) {
//formatting data correctly
String [] info = line.split(" ");
for(int i = 0; i < info.length; i++){
System.out.println(info[i]);
}
System.out.println(info[0]);
System.out.println(info[1]); /* this line of code */
}
So every info [] is of length 4. When I run the for loop, it prints out everything exactly as expected. Printing out "info[0]" works exactly has expected.
But for some reason when I attempt to print out "info[1]", I get an
java.lang.ArrayIndexOutOfBoundsException: 1
error. I have no explanation for why this happens. When I don't attempt to print out info[1] by itself, my program works correctly. In the for loop, info[1] gets printed out, because the for loop loops through 4 times. This code worked perfectly in Netbeans, but using Ant is doesn't work.
Does anyone know adding or removing just one line,
System.out.println(info[1]);
causes my program to run or throw an exception?
I'm running Ant 1.9.2
I'm running Java 1.7.0_17
I've checked this multiple times, so I'm pretty sure its not something I've made an erro ron. I'm a fairly experienced programmer, so I pretty confident it's not my error. It runs well in Netbeans. I don't have an explanation for the error.
edit 1. My code throws an error the second time threw the while loop. Printing out the info[] length, or the line itself works great with I don't print out line[1] by itself. It stills fails and throws an error when I printout the info[1] by itself.
Edit 2. @Millie Smith was on the right trail because my .txt file wasn't correctly formatted. Viewing it in notepad for some reason didn't display the extra space in between each line. http://pastebin.com/njjdSHHZ My correct pastebin. I was attempting to use code to strip out all of the blank space, commas, new lines, and such,
line = line.replaceAll("\\s+", " ");
line = line.replaceAll(",","");
line = line.replaceAll("\\n", "");
line = line.replaceAll("\\r", "");
String [] info = line.split(" ");
I incorrectly assumed that that code would take care of any irregularities. I was wrong. So on my first pastebin, I formatted the code to what I thought I was dealing with, which was also incorrect.
So if I test for line.length() > 1, that gives me my results that I am looking for. I'm not an experienced programmer as I think I am.