Results 1 to 6 of 6
Thread: Array Index out of bounds
- 04-28-2009, 11:38 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
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 BookTestLast edited by leapinlizard; 04-28-2009 at 11:55 PM.
-
Let's look at a plain Book entry in your data array:
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:{"Java", "Davis", "123", "Wiley", "London", "25.50", "Book"}
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.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]));Last edited by Fubarable; 04-29-2009 at 12:02 AM.
- 04-29-2009, 03:54 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
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
-
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.
- 04-29-2009, 04:48 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
Thank you so much i got it!
-
Similar Threads
-
[SOLVED] Array index out of bounds exception
By sruthi_2009 in forum New To JavaReplies: 5Last Post: 11-24-2010, 11:46 AM -
Array Index problems
By ragnor2004 in forum New To JavaReplies: 4Last Post: 03-26-2009, 07:53 PM -
[SOLVED] Search problem - array out of bounds
By viper110110 in forum New To JavaReplies: 5Last Post: 11-26-2008, 04:26 AM -
why is my array out of bounds?
By Phobos0001 in forum New To JavaReplies: 3Last Post: 03-24-2008, 01:20 AM -
problems with array index
By mary in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks