Results 1 to 10 of 10
- 08-31-2008, 10:08 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
ArrayIndexOutofBoundsException help
Hello again, I am having problems with my for loop in my program. It compiles ok but when i try to run it, it gives me the ArrayIndexOutofBoundsException. I really neep this help as soon as possible. It says my errors are on line 20 and 108
Java Code:import java.awt.*; import javax.swing.JOptionPane; class kbfirst { public static void main(String[] args) { int x = 0; int y = 0; int i; int j; int k = 0; int a; int b; int c = 0; String r; String s; String t; int z; z = RowSum(k); int q; q = ColSum(c); r = JOptionPane.showInputDialog(null, "Would you like to add a row or column???"); if (r == "row") { x = Integer.parseInt(r); x = 0; } else if (r == "column") { y = Integer.parseInt(r); y = 1; } else { System.out.println("Try again *** Try using lowercase letters"); } if (x == 0) { //Prompt the user to enter a string s = JOptionPane.showInputDialog(null, "You have chosen to add a row. Which row would you like to add row 1,2,3, or 4???"); if (s == "1") { k = Integer.parseInt(s); k = 1; System.out.println("Sum for row number " + k + "is" + z); } else if (s == "2") { k = Integer.parseInt(s); k = 2; System.out.println("Sum for row number " + k + "is" + z ); } else if ( s == "3") { k = Integer.parseInt(s); k = 3; System.out.println("Sum for row number " + k + "is" + z ); } else if (s == "4") { k = Integer.parseInt(s); k = 4; System.out.println("Sum for row number " + k + "is" + z ); } else { System.out.println("What did you do wrong????? Try again"); } } if (y == 1) { //Prompt the user to enter a string t = JOptionPane.showInputDialog(null, "You have chosen to add a column. Which column would you like to add column 1,2, or 3"); if (t == "1") { c = Integer.parseInt(t); c = 1; System.out.println("Sum for column number " + c + "is" + q); } else if (t == "2") { c = Integer.parseInt(t); c = 2; System.out.println("Sum for column number " + c + "is" + q); } else if (t == "3") { c = Integer.parseInt(t); c = 3; System.out.println("Sum for column number " + c + "is" + q); } else { System.out.println("What did you do wrong????? Try again"); } } } public static int ColSum(int chosencolumn1) { int[][] TwoDimArray = { {10, 20, 25 , 15}, {35, 0, 9, 5}, {45, 25, 55, 50} }; int Coltotal = 0; int m = chosencolumn1 - 1; for (int l = 0; l < TwoDimArray.length; l++) { Coltotal = Coltotal + TwoDimArray[l][m]; return Coltotal; } return 0; } public static int RowSum(int chosenrow2) { int[][] TwoDimArray = { {10, 20, 25 , 15}, {35, 0, 9, 5}, {45, 25, 55, 50} }; int Rowtotal = 0; int l = chosenrow2 - 1; for (int m = 0; m < TwoDimArray[chosenrow2].length-1; m++) { Rowtotal = Rowtotal + TwoDimArray[m][l]; return Rowtotal; } return -2; } }
-
Then the first thing you need to do is go to lines 20 and 108 and figure out why an array index (on line 108 -- the second one) is equal to -1.It says my errors are on line 20 and 108
Some hints:
Java Code:public static void main(String[] args) { //.... code deleted here int k = 0; //.... code deleted here int z; z = RowSum(k); // k here is 0, right?Java Code:public static int RowSum(int chosenrow2) // so this is 0 { int[][] TwoDimArray = { //.... code deleted here }; int Rowtotal = 0; int l = chosenrow2 - 1; // so what's l now if chosenrow2 is 0? for (int m = 0; m < TwoDimArray[chosenrow2].length - 1; m++) { Rowtotal = Rowtotal + TwoDimArray[m][l]; // *** here we throw the error! return Rowtotal; } return -2; }Last edited by Fubarable; 08-31-2008 at 10:38 PM.
- 08-31-2008, 10:39 PM #3
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
i cannot figure out it out. When i go through my for loop statemnts in my methods they work out fine but maybe im missing something. could you point me in the right direction
-
- 08-31-2008, 10:45 PM #5
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
Ok thanks. The reason i had k and those other integers equal to zero is because they kept asking me to initialize them. Thats the only way i got the error to go away. what should i change it to???
-
The smart-arse in me wants to say "whatever works", and truthfully I'd start there, but I'd really want to use what logic dictates it should be, and you should best know that. I didn't go through your program logic but only went through searching for the cause of your error.what should i change it to?
Oh, and please give your variables names that make sense.
- 08-31-2008, 10:57 PM #7
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
I'm really just beginning and things are just coming along for me. I appreciate all the input though. I'm gonna go through it when i actually get it to run and change most of the variables, kinda jumbled right now
- 09-01-2008, 01:15 PM #8
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
- 09-01-2008, 10:36 PM #9
Java Code:r = JOptionPane.showInputDialog(null, "Would you like to add " + "a row or column???"); // Use the [i]equals[/i] method to test for String equality. if (r.equals("row")) { // You'll have trouble trying to // parse "row" to an [i]int[/i] // x = Integer.parseInt(r); x = 0; } ... public static int getRowSum(int rowIndex) { int[][] twoDArray = { {10, 20, 25 , 15}, {35, 0, 9, 5}, {45, 25, 55, 50} }; int rowTotal = 0; for (int m = 0; m < twoDArray[rowIndex].length; m++) { rowTotal = rowTotal + twoDArray[rowIndex][m]; } return rowTotal; }
- 09-03-2008, 05:16 PM #10
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
java.lang.ArrayIndexOutOfBoundsException
By mensa in forum Java 2DReplies: 7Last Post: 05-05-2008, 09:09 AM -
java.lang.ArrayIndexOutOfBoundsException
By riccian in forum New To JavaReplies: 0Last Post: 03-18-2008, 09:38 AM -
ArrayIndexOutOfBoundsException
By daredavil82 in forum New To JavaReplies: 2Last Post: 12-14-2007, 09:29 PM -
java.lang.ArrayIndexOutOfBoundsException
By mew in forum New To JavaReplies: 2Last Post: 12-02-2007, 09:40 PM -
java.lang.ArrayIndexOutOfBoundsException
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 05:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks