Results 1 to 4 of 4
- 04-20-2012, 04:32 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Arrays
Ok so I am reading in a csv file to an array, but keep getting a out of bounds error (I know what this means) but cant work out why it is happening in my fields array.
Java Code:public static void main(String[] args) { BufferedReader in; String line; Statment[] Transactions = new Statment[127]; try { in = new BufferedReader(new FileReader("pays.csv")); int i=0; while ((line = in.readLine())!= null) { String[] fields = line.split(","); String date= fields[0]; String gross= fields[1]; String tax =fields[2]; String net =fields[3];// line 34 Transactions[i]= new Statment(date,gross,tax,net); i++; } } catch (IOException e){ System.out.println("There was a problem with the file");}
Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at pbankanalysis.BankAnalysisApp.main(BankAnalysisApp.java:34)
Last edited by Fubarable; 04-20-2012 at 04:39 AM. Reason: code tags added
-
Re: Arrays
Understand that you're walking a dangerous line when you use "magic numbers" to access your array as you're doing.
What happens if you put println statement in the code to test what the fields array actually holds before you start assigning its component parts off.
- 04-20-2012, 04:55 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Re: Arrays
Thanks for that I found the error it wasn't in my code as such it was my file it had blanks and my code wasn't handling null values. can you explain what you mean by magic numbers.
Stephen
-
Re: Arrays
Similar Threads
-
Copying Single Arrays to 2-D Arrays
By jmscarlet9 in forum New To JavaReplies: 7Last Post: 04-02-2012, 11:17 PM -
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02-17-2012, 12:49 PM -
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks