0
\$\begingroup\$

I have a method, which outputs multiplication table sized by given maximum number. Also, it visually separates first row and column from the rest table. How my code could be improved or optimized?

public class Example {
    public static void main(String args[]) {
        printMultiplicationTable(15);
    }

    private static void printMultiplicationTable(int max) {
        for (var row = 1; row <= max; row += 1) {
            if (row == 2) {
                for (var column = 2; column <= max; column += 1) {
                    System.out.print("----");
                }
                System.out.println();
                continue;
            }


            for (var column = 1; column <= max; column += 1) {
                int result = row * column;

                if (column == 1) {
                    if (row < 10) {
                        System.out.print(result + " |\t");
                    } else {
                        System.out.print(result + "|\t");
                    }

                } else {
                    System.out.print(result + "\t");
                }
            }

            System.out.println();
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ You don't care about the 2x row? Maybe you don't want to continue if row == 2. \$\endgroup\$ – glenn jackman Oct 5 '18 at 20:00
1
\$\begingroup\$

When I run your code I see:

1 |     2       3       4       5       6       7       8       9       10      11      12      13      14      15
--------------------------------------------------------
3 |     6       9       12      15      18      21      24      27      30      33      36      39      42      45
4 |     8       12      16      20      24      28      32      36      40      44      48      52      56      60
5 |     10      15      20      25      30      35      40      45      50      55      60      65      70      75
6 |     12      18      24      30      36      42      48      54      60      66      72      78      84      90
7 |     14      21      28      35      42      49      56      63      70      77      84      91      98      105
8 |     16      24      32      40      48      56      64      72      80      88      96      104     112     120
9 |     18      27      36      45      54      63      72      81      90      99      108     117     126     135
10|     20      30      40      50      60      70      80      90      100     110     120     130     140     150
11|     22      33      44      55      66      77      88      99      110     121     132     143     154     165
12|     24      36      48      60      72      84      96      108     120     132     144     156     168     180
13|     26      39      52      65      78      91      104     117     130     143     156     169     182     195
14|     28      42      56      70      84      98      112     126     140     154     168     182     196     210
15|     30      45      60      75      90      105     120     135     150     165     180     195     210     225

So row #2 is missing (as glenn jackman mentions), as well as both the "one times" rows and columns, which are usually present.

The horizontal divider is no-where near long enough for your output, since you are printing out 4 '-' characters, but using a tab character ('\t') to advance to the next column which typically advances to a multiple of 8. This can be corrected by using a formatted output, instead of just printing. Consider:

jshell> for (var i=1; i<=5; i++) {
   ...>   for (var j=1; j<=5; j++) {
   ...>     System.out.format(" %2d |", i*j);
   ...>   }
   ...>   System.out.println();
   ...> }
  1 |  2 |  3 |  4 |  5 |
  2 |  4 |  6 |  8 | 10 |
  3 |  6 |  9 | 12 | 15 |
  4 |  8 | 12 | 16 | 20 |
  5 | 10 | 15 | 20 | 25 |

Notice all the numbers are lined up on their right edge. No tab characters are being used; just %2d format code which outputs the number padded to a width of 2 with spaces on the left.

This allows you to avoid special casing of the of column 1, where you test for row < 10 to determine whether you need an extra space or not.

row += 1 and column += 1 are usually written as row++ and column++ in for loops.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.