Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Arrays

  1. #1
    sugst893 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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 05:39 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    sugst893 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Arrays

    Quote Originally Posted by sugst893 View Post
    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.
    You are accessing the array using hard-coded numbers (the magic numbers) rather than a variable in a for loop that will only loop to the maximum of the array.
    sugst893 likes this.

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-03-2012, 12:17 AM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •