0

Here is the code:Getting Array index out of Bound exception

class Max {
    public static void main(String args[]) {
        int a[][];
        Scanner src = new Scanner(System. in );
        System.out.println("Enter the no of rows");
        int rows = src.nextInt();
        a = new int[rows][5];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < 5; j++) {
                a[i][j] = src.nextInt();
            }
        }
        System.out.println("Array is");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print("  " + a[i][j]);
            }
            System.out.println();
        }
        int l = a[0][0];
        int i;
        for (i = 0; i < rows; i++)
            l = a[i][0];
        for (int j = 0; j < 5; j++)
            if (l < a[i][j])
                l = a[i][j];
        System.out.println("Max" + l);
    }
}

on run time it gives the following: Exception in thread "main" java.lang.Array index out of Bound Exception:3 at Max.main Max.java:33 Can Anyone suggest what is wrong in the code????

4
  • 1
    Your code is unreadable. Format it correctly. All Java IDEs can do that with a single keyboard shortcut. And tell us which line is 33. Commented Mar 2, 2014 at 9:08
  • I would step through your code in a debugger to debug you code and understand why it is not doing what you expect. Commented Mar 2, 2014 at 9:10
  • please use braces!!! (on the last for (i = 0; especially) Commented Mar 2, 2014 at 9:10
  • yes problem was with braces........and its solved Commented Mar 2, 2014 at 9:32

2 Answers 2

2

You miss a { after the for (i = 0; i < rows; i++) so that the for (int j = 0; j < 5; j++) loop is inside the first one.

Without that, this gives:

for (i = 0; i < rows; i++)
  l = a[i][0];
// End of the for i loop, now i = rows. 
for (int j = 0; j < 5; j++)
  if (l < a[i][j])  // i = rows: bang.
    l = a[i][j];
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

public static void main(String args[]) {
    int a[][];
    Scanner src = new Scanner(System. in );
    System.out.println("Enter the no of rows");
    int rows = src.nextInt();
    a = new int[rows][5];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 5; j++) {
            a[i][j] = src.nextInt();
        }
    }

    System.out.println("Array is");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 5; j++) {
            System.out.print("  " + a[i][j]);
        }
        System.out.println();
    }
    int l = a[0][0];
    int i;
    for (i = 0; i < rows; i++) {
        l = a[i][0];
        for (int j = 0; j < 5; j++)
            if (l < a[i][j])
                l = a[i][j];
    }
    System.out.println("Max" + l);
}

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.