Results 1 to 2 of 2
- 07-31-2007, 11:40 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
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
- 08-01-2007, 12:47 AM #2
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
-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By paul in forum New To JavaReplies: 8Last Post: 03-05-2011, 04:53 AM -
java.lang.ArrayIndexOutOfBoundsException
By riccian in forum New To JavaReplies: 0Last Post: 03-18-2008, 10:38 AM -
java.lang.ArrayIndexOutOfBoundsException
By mew in forum New To JavaReplies: 2Last Post: 12-02-2007, 10:40 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 11:55 PM -
java.lang.ArrayIndexOutOfBoundsException
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 06:15 AM
Bookmarks