Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 3.33 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-07-2005, 12:00 AM   PM User | #1
finittz
New to the CF scene

 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
finittz is an unknown quantity at this point
Java: 2-D array multiplication table problems

hi, im a fairly new java programmer...

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 dont get that, anyone got any ideas? anywayz heres my code take a look....

// 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
finittz is offline   Reply With Quote
Old 01-07-2005, 12:12 AM   PM User | #2
cfc
Regular Coder

 
Join Date: Dec 2004
Location: Keswick, Ontario
Posts: 251
Thanks: 0
Thanked 0 Times in 0 Posts
cfc is an unknown quantity at this point
Quote:
Originally Posted by finittz
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
{
ArrayIndexOutOfBoundsException indicates that the array index (the number representing the element you're trying to interact with) is outside of the element indices in the array.

From a quick inspection, you should be using the less than operator rather than the less than or equal to operator in your for statements. The reason for this is that array indices start at zero while the variables a and b specify the number of elements in the array and hence will be one larger than the highest array index.
cfc is offline   Reply With Quote
Old 01-07-2005, 01:59 AM   PM User | #3
finittz
New to the CF scene

 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
finittz is an unknown quantity at this point
ok now that i've gotten that solved heres my renewed program...except now the problem is with alignment of the numbers...i need them to be centerly aligned, any ideas?

// 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 ();

a++;
b++;
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
finittz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:35 AM.


Advertisement
Log in to turn off these ads.