0

I've got a program where the user is giving the program the size of the array ie(the column and row size) and I am trying to give every position in the array the same value. I'm having an issue with my loop though, here it is.

for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            //CODE
        }   
    } 

I can see that the issue is I am trying to give a value to a position that doesn't exist but I have no idea how to work around this issue. Any help would be appreciated :)

3
  • 1
    Before assign the value you need to allocate memory for the array. Commented May 20, 2014 at 8:13
  • 1
    Did you create your array with new Whatever[row][col]? Commented May 20, 2014 at 8:13
  • Its in Java :) Thanks for the quick reply btw. Commented May 20, 2014 at 8:14

6 Answers 6

1

Try working with length, not with user input:

  // ask user for sizes
  int col = ...;
  int row = ...;

  // declare the array, let it be of type int 
  // it's the last occurence of "row" and "col"
  int[][] data = new int[row][col];

  // loop the array    
  for (int r = 0; r < data.length; ++r) { // <- not "row"!
    int[] line = data[r];

    for (int c = 0; c < line.length; ++c) { // <- not "col"!
      // do what you want with line[c], e.g. 
      // line[c] = 7; // <- sets all array's items to 7
    }
  }

working with actual array's dimensions just prevent you from accessing non-existing items

Sign up to request clarification or add additional context in comments.

Comments

0

From the code snippet you provided it seems you are fine. Maybe the array is not well initialized or you mismatched the row and column numbers. Try to use more specific variable names than 'i' and 'j'.

Comments

0

in java

try{
  for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            //CODE
        }   
    }
}catch(IndexOutOfBoundsException exp){
   System.out.printlv(exp.getMessage());
}

Comments

0

To begin, what makes you say that you "can see that the issue is I am trying to give a value to a position that doesn't exist"? What symptoms are you seeing that makes you believe this?

On the face of it, your code looks fine, however (and this is a big however) you have omitted the most important bits of code, viz the declaration of your 2D array, and the part inside the loop body where you assign the value to the array member. If you add these then I, or somebody else, may be able to help further.

4 Comments

My program only throws the exception, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 if I have two uneven inputs into the program. ie cols is 5 and row is 6.
So from what I can tell its looping over and trying the add a value to the position of [5][5] for example which doesn't exist...?
Go on, be a hero and give us the whole bit of code :-) From this exception, it looks like you're either: 1) Declared the array too small; 2) Got incorrect values for 'row' or 'col', or 3) Used the wrong indices in your assignment (a common error is to have them the wrong way around). You'd really make life a lot easier for us if you gave the code though. This isn;t meant to be a guessing game and you are uiung people's valuable time.
So if it works when row and col are the same, then it sounds like you are using them the wrong way around in your assignment.
0

Solution:

int matriz[][] = new int [row][col];
for(int i = 0; i <row; i++){
    for(int j = 0; j < col; j++){
        matriz[i][j] = 0; 
    }   
} 

Comments

0
//try this one 
import java.util.Scanner;  //Scanner class required for user input.

class xyz
{
  public static void main(String ar[])
  {
     int row,col;
     Scanner in=new Scanner(System.in); //defining Object for scanner class
     System.out.println("Enter row");
     row=in.nextInt();
     System.out.println("Enter Column");
     col=in.nextInt();
     int mat[][]=new int[row][col];    //giving matrix size
     for(int i=0;i<row;i++)
     {
        for(int j=0;j<col;j++)
        {
            mat[i][j]=0;   //or any value decided by you
        }
     }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.