1

I have this code but i keep getting this error message and i dont know why?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at javaapplication28.JavaApplication28.main(JavaApplication28.java:38) 1 2 Java Result: 1

public static void main(String[] args) {   

    Scanner input = new Scanner(System.in);
        System.out.println("This program transposes a matrix.");
        System.out.println("Please enter the number of rows");
        int rows = input.nextInt();
        System.out.println("User enters: "+rows);
        System.out.println("Please enter the number of columns");
        int columns = input.nextInt();
        System.out.println("User enters: "+columns);
        int [][]matrix=new int[rows][columns];
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[i].length;j++){
             System.out.print("Enter value for row [" +i+ "] column [" +j+"]:");
                matrix[i][j]=input.nextInt();  
            }
        }
        for(int i=0;i<=matrix.length;i++){
             System.out.println();
             for(int j=0;j<=matrix.length;j++){
                 System.out.print(matrix[i][j]+"  ");           
             }
         }
        System.out.println("The transpose of this matrix has" +columns+"rows and"+rows+"columns and the transpose is:");
        for(int i=0;i<=matrix.length;i++){
             System.out.println();
             for(int j=0;j<=matrix.length;j++){
                 System.out.print(matrix[j][i]+"  ");
    }
}}
}

2 Answers 2

3

You run in loop from 0 to length+1 i<=matrix.length. Remove = from for statement and on internal loop add: matrix[i].length instead matrix.length to get column count and not row.

Here is valid code:

for(int i=0;i<matrix.length;i++){
        System.out.println();
        for(int j=0;j<matrix[i].length;j++){
            System.out.print(matrix[i][j]+"  ");           
        }
    }
    System.out.println("The transpose of this matrix has" +columns+"rows and"+rows+"columns and the transpose is:");
    for(int i=0;i<matrix.length;i++){
        System.out.println();
        for(int j=0;j<matrix[i].length;j++){
            System.out.print(matrix[j][i]+"  ");
        }
    }}
Sign up to request clarification or add additional context in comments.

3 Comments

For the general case (ragged array) shouldn't that be j<matrix[i].length?
Also, the transpose code is wrong, you can't just swap the indexes like that in the general case unless the matrix is always square.
@JimGarrison well, if you entered to second for i=0; so you can use i or 0. The same thing. True, that to use indexes is not good technique. For this example he has square matrix. Otherwise i could write by other way.
0

If you start your for cycle index iterators at 0 you should use < and not <= when it's the length of an array, because otherwise it would be as if you're trying to get the (n+1)th element of the (size n) array what it's causing the ArrayIndexOutOfBoundsException exception

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.