Results 1 to 2 of 2
  1. #1
    fernando is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Error: java.lang.ArrayIndexOutOfBoundsException

    Hi, I want to make a multiplication table where the user can choose how many rows and columns the table has, i get a "java.lang.ArrayIndexOutOfBoundsException: 5
    at Review.main(Review.java:26)" error...I don't get that, anyone got any ideas? anyways here's my code take a look

    Java Code:
    // The "Review" class.
    import java.awt.*;
    import hsa.Console;
    
    public class Review
    {
    static Console c; // The output console
    
    public static void main (String[] args)
    {
    c = new Console ();
    
    int a, b;
    c.print ("a/row:");
    a = c.readInt ();
    c.print ("b/col:");
    b = c.readInt ();
    
    
    int table[] [] = new int [a] [b]; // max for table
    
    for (int row = 1 ; row <= a ; row++) //length of rows
    {
    for (int col = 1 ; col <= b ; col++) //length of columns
    {
    table [row] [col] = row * col;
    c.print (table [row] [col] + " ");
    
    }
    c.println ();
    }
    
    
    } // main method
    } // Review class
    Thanks.

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

    Default

    The first element of an array is at index = 0
    The last element is at index = array.length -1
    Trying to access an element at index = array.length will throw an ArrayIndexOutOfBoundsException.
    Try this:
    Java Code:
    for (int row = 0 ; row < a ; row++) //length of rows
    {
        for (int col = 0 ; col < b ; col++) //length of columns
        {
            table [row] [col] = row * col;
            c.print (table [row] [col] + " ");

Similar Threads

  1. Replies: 8
    Last Post: 03-05-2011, 03:53 AM
  2. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 09:38 AM
  3. java.lang.ArrayIndexOutOfBoundsException
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-02-2007, 09:40 PM
  4. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  5. java.lang.ArrayIndexOutOfBoundsException
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 05: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
  •