0

I am trying to print this loop into a table under N 10*N 100*N etc. I can get the loop to work but I can't figure out the spacing to move my values to the right. I am newer at this so any help would be appreciated.

package Exercises;

public class TabularOutput {

    public static void main(String[] args)
    {
        //initialize variables
        int w = 0;
        int x = 0;
        int y = 0;
        int z = 0;

        System.out.println("N     10*N     100*N     1000*N\n");

        while(w<=4)
        {
            int a = w + 1;
            System.out.println(a);
            ++w;
        }
        while(x<=4)
        {   
            int b = (x + 1) * 10;
            System.out.println(b);
            ++x;
        }
        while(y<=4)
        {
            int c = (y + 1) * 100;
            System.out.println(c);
            ++y;
        }
        while(z<=4)
        {
            int d = (z + 1) * 1000;
            System.out.println(d);
            ++z;
        }


    }//end method main

}//end class TabularOutput
Barmar
  • 596,455
  • 48
  • 393
  • 495
Joseph Kraemer
  • 111
  • 1
  • 2
  • 10
  • What you are asking for is known as String padding. [This](http://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java) is a prior question about it that you can read through. – Compass Sep 18 '14 at 18:26
  • The problem is that you're printing things in the wrong order. You need to print N, 10*N, 100*N, and 1000*N on the same line, not in separate loops for each. – Barmar Sep 18 '14 at 18:28

1 Answers1

2

Try this:

 int a=0, b=0, c=0, d=0;

 while(w<=4)
    {
        a = w + 1;
        System.out.print(a);
        ++w;

        b = (x + 1) * 10;
        System.out.print("\t"+b);
        ++x;

        c = (y + 1) * 100;
        System.out.print("\t"+c);
        ++y;

        d = (z + 1) * 1000;
        System.out.println("\t"+d);
        ++z;

    }
AndreDuarte
  • 694
  • 5
  • 20