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
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;
}
}