-2

I'm currently trying to use java to make a multitable with the following output:

    0  1  2  3  4  5  6  7  8  9
 +------------------------------
0|  0  0  0  0  0  0  0  0  0  0
1|  0  1  2  3  4  5  6  7  8  9
2|  0  2  4  6  8 10 12 14 16 18
3|  0  3  6  9 12 15 18 21 24 27
4|  0  4  8 12 16 20 24 28 32 36
5|  0  5 10 15 20 25 30 35 40 45
6|  0  6 12 18 24 30 36 42 48 54
7|  0  7 14 21 28 35 42 49 56 63
8|  0  8 16 24 32 40 48 56 64 72
9|  0  9 18 27 36 45 54 63 72 81

However, instead of the result above, i got this:

   0   1   2   3   4   5   6   7   8   9
 +-------------------------------------------
0| 0  0  0  0  0  0  0  0  0  0
1| 0  1  2  3  4  5  6  7  8  9
2| 0  2  4  6  8  10  12  14  16  18
3| 0  3  6  9  12  15  18  21  24  27
4| 0  4  8  12  16  20  24  28  32  36
5| 0  5  10  15  20  25  30  35  40  45
6| 0  6  12  18  24  30  36  42  48  54
7| 0  7  14  21  28  35  42  49  56  63
8| 0  8  16  24  32  40  48  56  64  72
9| 0  9  18  27  36  45  54  63  72  81

Therefore, i'd like to know is there any way the fix the problem? Thanks.

Here's the code.

import java.util.*;
public class MultiTable 
{
    public static void main(String[] args) 
    {
        int x = 9;
        System.out.print("   ");
        for(int k = 0; k<=x ;k++ ) {
            System.out.print( k + "   ");
        }

       System.out.println("\n +-------------------------------------------");
        for(int i = 0 ;i<=x ;i++) {
             System.out.print(i+ "| ");
            for(int j=0;j<=x ;j++) {
             System.out.print(j*i + "  ");
            }
            System.out.println();
        }
        System.out.println("\n"); 
        System.out.println("\n\n");
    }
}

Thanks.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
  • It seems that the header labels are separated by 3 space characters, and the values only by 2 . – Arnaud Oct 14 '19 at 15:11

4 Answers4

0

you can add space to one-digit numbers, but it will work only if your maximal number is two-digit. To handle this problem you should calculate length of your maximal number and adjust length of others with spaces.

for(int j=0;j<=x ;j++) {
    String resStr0 = Integer.valueOf(j*i).toString();
    String resStr = resStr0.length() < 2 ? resStr0+" " : resStr0;
    System.out.print(resStr + "  ");
}

   0   1   2   3   4   5   6   7   8   9   
 +-------------------------------------------
0| 0   0   0   0   0   0   0   0   0   0   
1| 0   1   2   3   4   5   6   7   8   9   
2| 0   2   4   6   8   10  12  14  16  18  
3| 0   3   6   9   12  15  18  21  24  27  
4| 0   4   8   12  16  20  24  28  32  36  
5| 0   5   10  15  20  25  30  35  40  45  
6| 0   6   12  18  24  30  36  42  48  54  
7| 0   7   14  21  28  35  42  49  56  63  
8| 0   8   16  24  32  40  48  56  64  72  
9| 0   9   18  27  36  45  54  63  72  81  
chlebek
  • 2,431
  • 1
  • 8
  • 20
0

Print formatted strings instead of printing it as-is.

Either you can print the formatted string directly to the output using System.out.printf()

or

You can format a string using String.format() first, then print it using println.

In your case, you need to force the integers to be at least 2 characters long. If the integer has too few digits, you can pad it in spaces. This is how you can do it:

int number = 1;

// printf approach
System.out.printf("%3d", number);

// Formatted string approach
String formatted = String.format("%3d", number);
System.out.print(formatted);

In the format, you can see %3d. It means:

  • % start the formatting
  • 3 right-align, 3 spaces (negative number will left-align). Im using 3 so that the numbers won't stick together.
  • d a decimal (base-10 integer) will be formatted

You can read more about formatters in Java's official docs

sweet suman
  • 1,031
  • 8
  • 18
0
  1. numbers control the space padding to their left (opposite to the code, which adds padding to the right)

  2. if the number is two digits, you pad with one space, otherwise, two spaces

    for(int i = 0 ;i<=x ;i++) {
         System.out.print(i+ "|");  // leave padding to the number printing loop
         for(int j = 0 ; j <= x ; j++) {
             int numbertoPrint = j*i;
             int spacePadding = j == 0 || numbertoPrint >= 10 ? 1 : 2;  // one space at the beginning or if two digit number
             System.out.print(" ".repeat(spacePadding) + numbertoPrint);  // repeat() available since java 11
         }
         System.out.println();
    }
    
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
0

You can update your code as below, to get desired output. Changes are below:

1) 1st System.out.println should have extra space.
2) Change the logic to print the space before the number, rather than after it to decide he no of spaces for it.
2) Inside the inner for loop add the if condition, to decide the number of spaces, depending on the basis of length of the corresponding number after it.

import java.util.*;
public class MultiTable 
{
    public static void main(String[] args) 
    {
      public static void main(final String[] args) {
    final int x = 9;
    System.out.print("    ");
    for (int k = 0; k <= x; k++) {
      System.out.print(k + "  ");
    }

    System.out.println("\n +------------------------------");
    for (int i = 0; i <= x; i++) {
      System.out.print(i + "|");
      for (int j = 0; j <= x; j++) {
        if (String.valueOf(j * i).length() == 1) {
          System.out.print("  " + j * i);
        } else {
          System.out.print(" " + j * i);
        }
      }
      System.out.println();
    }
    System.out.println("\n");
    System.out.println("\n\n");
  }
}

Output Of Code:

    0  1  2  3  4  5  6  7  8  9  
 +------------------------------
0|  0  0  0  0  0  0  0  0  0  0
1|  0  1  2  3  4  5  6  7  8  9
2|  0  2  4  6  8 10 12 14 16 18
3|  0  3  6  9 12 15 18 21 24 27
4|  0  4  8 12 16 20 24 28 32 36
5|  0  5 10 15 20 25 30 35 40 45
6|  0  6 12 18 24 30 36 42 48 54
7|  0  7 14 21 28 35 42 49 56 63
8|  0  8 16 24 32 40 48 56 64 72
9|  0  9 18 27 36 45 54 63 72 81
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47