Results 1 to 6 of 6
  1. #1
    leapinlizard is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default Array Index out of bounds

    Hi, someone please help me trace out the problem because I tried everything still can't figure out where's the problem ...
    : 7 Line 23

    Java Code:
    public class BookTest
    {
    	public static void main(String args[])
    
    	{
                   Book bookArray[] = new Book[10]; 
                    
    	 	String dataArray [][] = {{"Java","Davis","123","Wiley","London","25.50","Book"},
    					 {"Where's my car", "Aston Kutcher","777","ORiely","Dallas","5.95","Fiction","123","456"},
    					 {"Earthquakes","ChuckBerry","435","LABooks","LA","75.00","NonFiction"},
    					 {"C++","Jones","456","Random","NY","10.75","Book"},
    					 {"Doom","The Rock","918","Sans Publishing","LA","12.50","Fiction","1233","2323"}, 
    					 {"Universal Studios","Walt","987","Dell","Houston","29.90","NonFiction"}};
    	
                    for (int i=0; i<bookArray.length; i++)
                    {
                    	
                             bookArray[i]=new Fiction (dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),i, new Background(dataArray[i][7], dataArray[i][8]));
       
      			    
     			if (dataArray[i][6].equals("Fiction"))
    			
    			  bookArray[i]=new Fiction (dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),i, new Background(dataArray[i][7], dataArray[i][8]));
                            
    
                             
    			if (dataArray[i][6].equals("NonFiction"))
    			
                              bookArray[i]= new NonFiction(dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),"History");
                                                     
    
    			
    
    
    		}// end of FOR Loop
    
    
                
             	
                    for (int j=0; j<10;j++)
                        {
                      //  bookArray[j].calculateTotalCharge((j+2)*3,Double.parseDouble(dataArray[j][5]));
                     
    
                 	    System.out.println();
    		    System.out.println();
                        System.out.printf(bookArray[j].toString());
    
                        } //end of for with j  
    
    	               
           } //end of main
    } //end class BookTest
    Last edited by leapinlizard; 04-29-2009 at 12:55 AM.

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

    Default

    Let's look at a plain Book entry in your data array:
    Java Code:
    {"Java", "Davis", "123", "Wiley", "London", "25.50", "Book"}
    You'll notice that if your data array entry is a plain Book, it only has 7 Strings in its array, meaning the array goes from [i][0] to [i][6] (this is because arrays start at 0 -- are "0" based). If you try to go beyond this with a plain Book, you will run out of data and this exception will be thrown. For instance, here:
    Java Code:
          if (dataArray[i][6].equals("Book"))
            bookArray[i] = new Fiction(dataArray[i][0], dataArray[i][1], 
              Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],
              dataArray[i][4]), Double.parseDouble(dataArray[i][5]), i,
    
              // *** this last line will throw the exception ***
              new Background(dataArray[i][7], dataArray[i][8]));
    This probably shouldn't be a new Fiction object here but instead should be a new Book object. Likely your Book constructor (which we never see) doesn't ask for a Background object. At least that's my guess.
    Last edited by Fubarable; 04-29-2009 at 01:02 AM.

  3. #3
    leapinlizard is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    well still doing same thing or may be i don't understand

    Java Code:
     
    public class BookTest
    {
    	public static void main(String args[])
    
    	{
                   Book bookArray[] = new Book[10]; 
                    
    	 	/*String dataArray [][] = {{"Java","Davis","123","Wiley","London","25.50","Book"},
    					 {"Where's my car", "Aston Kutcher","777","ORiely","Dallas","5.95","Fiction","123","456"},
    					 {"Earthquakes","ChuckBerry","435","LABooks","LA","75.00","NonFiction"},
    					 {"C++","Jones","456","Random","NY","10.75","Book"},
    					 {"Doom","The Rock","918","Sans Publishing","LA","12.50","Fiction","1233","2323"}, 
    					 {"Universal Studios","Walt","987","Dell","Houston","29.90","NonFiction"}};
     
     */
    
    	 	String dataArray [][] = {{"Where's my car", "Aston Kutcher","777","ORiely","Dallas","5.95","Fiction","123","456"},
    				         {"Earthquakes","ChuckBerry","435","LABooks","LA","75.00","NonFiction"},
    					 {"Doom","The Rock","918","Sans Publishing","LA","12.50","Fiction","1233","2323"}, 
    					 {"Universal Studios","Walt","987","Dell","Houston","29.90","NonFiction"}};
    		
     
    
                   for (int i=0; i<bookArray.length; i++)
                    {
                    	
                             bookArray[i]=new Fiction (dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),i, new Background(dataArray[i][7], dataArray[i][8]));
       
      			    
     			if (dataArray[i][6].equals("Fiction"))
    			
    			  bookArray[i]=new Fiction (dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),i, new Background(dataArray[i][7], dataArray[i][8]));
                            
    
                             
    			if (dataArray[i][6].equals("NonFiction"))
    			
                              bookArray[i]= new NonFiction(dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),"History");
                                                     
    
    			
    
    
    		}// end of FOR Loop
    
    
                
             	
                    for (int j=0; j<10;j++)
                        {
                      //  bookArray[j].calculateTotalCharge((j+2)*3,Double.parseDouble(dataArray[j][5]));
                     
    
                 	    System.out.println();
    		    System.out.println();
                        System.out.printf(bookArray[j].toString());
    
                        } //end of for with j  
    
    	               
           } //end of main
    } //end class BookTest

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

    Default

    Print out your code onto paper and with a pencil walk through what your code will do each step of the way. You'll see that every book is assigned to into a Fiction object whether it's a Fiction book or not. Then you'll know how to fix this.

    Best of luck.

  5. #5
    leapinlizard is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    Thank you so much i got it!

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

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 11:16 AM
  2. Array Index problems
    By ragnor2004 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 08:53 PM
  3. [SOLVED] Search problem - array out of bounds
    By viper110110 in forum New To Java
    Replies: 5
    Last Post: 11-26-2008, 05:26 AM
  4. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 02:20 AM
  5. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:30 PM

Posting Permissions

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