Oreo
Oreo

Reputation: 33

How to I equally space integers in a loop?

I have to print a multiplication table based on the user's input. Here's my code so far:

import java.util.Scanner;
public class problem7
{
public static void main (String args[])
{

int n1,n2,n3;
int i1, i2;
char again;
Scanner input=new Scanner(System.in);


System.out.println("Please enter the starting number:");
n1=input.nextInt();
System.out.println("\nPlease enter the ending number:");
n2=input.nextInt();

System.out.print("\n     ");
    for (i1=1; i1<=10; i1++){

        System.out.print("    "+i1);
    }
    for (n1=n1; n1<=n2; n1++ ){
        System.out.print("\n    "+n1);

        for (i2=1; i2<=10; i2++){
            n3=n1*i2;
            System.out.print("    "+n3);

        }
    }
}
}

here's the output:

Please enter the starting number:
2

Please enter the ending number:
4

         1    2    3    4    5    6    7    8    9    10
    2    2    4    6    8    10    12    14    16    18    20
    3    3    6    9    12    15    18    21    24    27    30
    4    4    8    12    16    20    24    28    32    36    40

the spacing gets messed up once there's an integer with 2 significant figures. How do i fix this? Thank you!

Upvotes: 1

Views: 2130

Answers (4)

Matin Rahman
Matin Rahman

Reputation: 1

import java.util.Scanner;
public class HelloWorld
{
public static void main (String args[])
{

int n1,n2,n3;
int i1, i2;
char again;
Scanner input=new Scanner(System.in);


System.out.println("Please enter the starting number:");
n1=input.nextInt();
System.out.println("\nPlease enter the ending number:");
n2=input.nextInt();

System.out.print("\n     ");
    for (i1=1; i1<=10; i1++){
        if(i1>9)
            System.out.print("   "+i1);
            else
        System.out.print("    "+i1);
    }
    for (n1=n1; n1<=n2; n1++ ){

        System.out.print("\n    "+n1);

        for (i2=1; i2<=10; i2++){
            n3=n1*i2;
            if(n3>9)
            System.out.print("   "+n3);
            else
            System.out.print("    "+n3);

        }
    }
}
}

This is how it should be like. Just remove one space as there are 2 characters in numbers which are greater than 9. So to format it in a correct way all you need to do is remove an space when you are printing.

Upvotes: 0

Keval
Keval

Reputation: 1859

Just use \t when ever you want to put some space .. try below code

  public static void main(String[] s) {
    int n1, n2, n3;
    int i1, i2;
    char again;
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the starting number:");
    n1 = input.nextInt();
    System.out.println("\nPlease enter the ending number:");
    n2 = input.nextInt();

    System.out.print("\n\t");
    for (i1 = 1; i1 <= 10; i1++) {

        System.out.print("\t" + i1);
    }
    for (n1 = n1; n1 <= n2; n1++) {
        System.out.print("\n\t" + n1);

        for (i2 = 1; i2 <= 10; i2++) {
            n3 = n1 * i2;
            System.out.print("\t" + n3);

        }
    }
 }

by this code output will be

    1   2   3   4   5   6   7   8   9   10
1   1   2   3   4   5   6   7   8   9   10
2   2   4   6   8   10  12  14  16  18  20
3   3   6   9   12  15  18  21  24  27  30
4   4   8   12  16  20  24  28  32  36  40

Upvotes: 0

MONTS_MIND_Hacker
MONTS_MIND_Hacker

Reputation: 1727

http://www.tutorialspoint.com/compile_java_online.php

import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
    int n1,n2,n3;
    int i1, i2;
    char again;
    Scanner input=new Scanner(System.in);
    System.out.println("Please enter the starting number:");
    n1=input.nextInt();
    System.out.println("\nPlease enter the ending number:");
    n2=input.nextInt();
    System.out.print("\n     ");
    for (i1=1; i1<=10; i1++){   
        System.out.print(String.format("    %2d",i1));
    }
    for (n1=n1; n1<=n2; n1++ ){
        System.out.print("\n    "+n1);
        for (i2=1; i2<=10; i2++){
            n3=n1*i2;
            System.out.print(String.format("    %2d", n3));  
        }
    }
}
}

Upvotes: 0

The easy-to-understand way: print an extra space if the number is less than 10.

System.out.print("\n    ");
if(n1 < 10) System.out.print(" ");
System.out.print(n1);

The shorter way: String.format can format numbers in several different ways, and one of the things it can do is automatically add spaces at the start:

System.out.println("\n    " + String.format("%2d", n1));
// alternatively:
System.out.println(String.format("\n    %2d", n1));

alternatively, System.out.printf combines the format and println calls:

System.out.printf("\n    %2d", n1);

Remember to do the same thing when printing i1 and n3!

Upvotes: 1

Related Questions