Results 1 to 10 of 10
  1. #1
    filly444 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

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

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

    Default

    It says my errors are on line 20 and 108
    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.

    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 11:38 PM.

  3. #3
    filly444 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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

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

    Default

    Quote Originally Posted by filly444 View Post
    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
    I have revised my post. Have you read it? Does it make sense to you?

  5. #5
    filly444 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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???

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

    Default

    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.

    Oh, and please give your variables names that make sense.

  7. #7
    filly444 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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

  8. #8
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    ...
    Oh, and please give your variables names that make sense.
    And follow the code conventions. Variables names always should start with a lowercase letter. Class names with a capital letter, and method names also with a lowercase letter.
    I die a little on the inside...
    Every time I get shot.

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

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

  10. #10
    filly444 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    I got the program to run with no problems. thanks for all the help. You will see me again

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 10:09 AM
  2. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 10:38 AM
  3. ArrayIndexOutOfBoundsException
    By daredavil82 in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 10:29 PM
  4. java.lang.ArrayIndexOutOfBoundsException
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-02-2007, 10:40 PM
  5. java.lang.ArrayIndexOutOfBoundsException
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:15 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
  •