-4

I'm just a newbie when it comes to programming, and a student. I was tasked to create a 2-D Array of a multiplication table but I seem to get the same error evertime: java.lang.arrayindexoutofboundsexception 10

Pls help.

Heres the code:

 public class MulTtable {

// rows and columns are declared as constants
 static final int ROWS= 10;
 static final int COLUMNS= 10;

// prints the content of the 2-D array
public static void printTable(char mt[][]){
int n=ROWS;


    for (int row = 0; row < ROWS; row++){

        for (int COLUMNS = 0; COLUMNS < COLUMNS; COLUMNS++){
        {
            System.out.print(mt[ROWS][COLUMNS] + "\t");
        }
        System.out.println();

    }
}
}

public static void main(String[] args){

    int mTable[][]= new int[ROWS][COLUMNS];

    for ( int ROWS = 0; ROWS < mTable.length; ROWS++){ 

        for ( int COLUMNS = 0; ROWS < mTable[ROWS].length; COLUMNS++){
            if (ROWS<11) // stores integer 1+1 in the first row of the array
              mTable[ROWS][COLUMNS] = 1+1;
              else
              mTable[ROWS][COLUMNS] = (ROWS)* (COLUMNS);


            }
        }


    }

}

Cheers,

Me

Thank you!

TungstenX
  • 830
  • 3
  • 19
  • 40
Zazo Rean
  • 1
  • 1
  • 5
    How is C++ related to your question? – Algirdas Preidžius Nov 17 '16 at 12:41
  • Hint: all your loops which declare *local* variables called `ROWS` and `COLUMNS` are pretty broke, IMO. The loop condition of `COLUMNS < COLUMNS` is particularly suspect. (You need to differentiate between `ROWS` and `COLUMNS` which are constants, the *count* of rows and columns, and ideally variables called `row` and `column` which indicates which row/column you're dealing with at the moment. Now is also a good time to format your code properly - your IDE should be able to do that for you. – Jon Skeet Nov 17 '16 at 12:43
  • Have you tried debugging it to see what is happening? – n247s Nov 17 '16 at 12:44
  • `for ( int COLUMNS = 0; ROWS < mTable[ROWS].length; COLUMNS++){` this will cause it. you increase columns but the"if" is with rows – XtremeBaumer Nov 17 '16 at 12:45
  • Look at Java naming convention. Also your have static final class members called ROWS and COLUMNS and local method members called the same; the compiler will sort it out but it is difficult to read and easy to make mistakes – TungstenX Nov 17 '16 at 12:46

3 Answers3

0

it wont print anything because you have one { too muc in your print table method and your naming has to change... working code:

static final int ROWS = 10;
    static final int COLUMNS = 10;

    public static void main(String[] args) {

        int mTable[][] = new int[ROWS][COLUMNS];

        for (int ROWS = 0; ROWS < mTable.length; ROWS++) {

            for (int COLUMNS = 0; COLUMNS < mTable[ROWS].length; COLUMNS++) {
                if (ROWS < 11)
                    mTable[ROWS][COLUMNS] = 1 + 1;
                else
                    mTable[ROWS][COLUMNS] = (ROWS) * (COLUMNS);

            }
        }
        printTable(mTable);
    }

    public static void printTable(int[][] mTable) {

        for (int row = 0; row < ROWS; row++) {

            for (int col = 0; col < COLUMNS; col++) {
                System.out.print(mTable[row][col] + "\t");
            }
            System.out.println();

        }
    }
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
0

First important thing, your loop variables are hiding the class-static variable. This is a bad code. ROWS is also a for-loop counter and a static class member. The code will work fine, but you'll get confused as to what you are trying to refer.

Now in the print method, there is a arrayIndexOutOfBound i.e. you are accessing element out of the boundaries of array

 for (int row = 0; row < ROWS; row++){
    for (int COLUMNS = 0; COLUMNS < COLUMNS; COLUMNS++){
        System.out.print(mt[ROWS][COLUMNS] + "\t");
    System.out.println();

In the innermost sys-out statement, you are refering to ROWS (capital), you need to use row i.e. the outer loop variable.

Also change the inner for-loop variable name . the condition COLUMNS < COLUMNS is totally confusing;

mtk
  • 13,221
  • 16
  • 72
  • 112
0

Here is one that prints a propper multiplication table:

public class MulTtable 
{
    static final int ROWS = 15;
    static final int COLUMNS = 15;

    public static void printTable(int mt[][])
    {
        for(int r = 0; r < mt.length; r++)
        {
            for(int c = 0; c < mt[r].length; c++)
            {
                {
                    System.out.print(mt[r][c] + "\t");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args)
    {
        int mTable[][] = new int[ROWS][COLUMNS];
        for(int r = 0; r < ROWS; r++)
        {
            for(int c = 0; c < COLUMNS; c++)
            {
                if(r < 1)
                {
                    mTable[r][c] = c;
                }
                else if(c < 1)
                {
                    mTable[r][c] = r;
                }
                else
                {
                    mTable[r][c] = r * c;
                }
            }
        }
        printTable(mTable);
    }
}
Mathis
  • 187
  • 1
  • 2
  • 10