Results 1 to 8 of 8
  1. #1
    iriscience is offline Member
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    11
    Rep Power
    0

    Default [Q] Spacing Help

    Hello! I am having serious issues with trying to get the spacing on my homework to look right. I have to print out a triangle that looks like this:
    Java Code:
                                       1
                                   1   2   1
                               1   2   4   2   1
                           1   2   4   8   4   2   1
                       1   2   4   8  16   8   4   2   1
                   1   2   4   8  16  32  16   8   4   2   1
               1   2   4   8  16  32  64  32  16   8   4   2   1
           1   2   4   8  16  32  64 128  64  32  16   8   4   2   1
    I can get the triangle to print the numbers correctly and form the left side of the triangle. I just cannot get the numbers to line up! Any help is greatly appreciated! Thanks guys!

    PS: Here is the code I have so far. The object was to make it using for loops.
    Java Code:
       public static void main(String[] args)
       {
          //Declare variables
          int lengthOfLine;
    
          //Get input from user
          Scanner inputFromUser = new Scanner(System.in);
          System.out.print("Enter a positive number: ");
          int height = inputFromUser.nextInt();
    
          lengthOfLine = 1;
          for (int count = 0; count < height; count++)
          {
             for (int numberOfSpacesForLine = 1; numberOfSpacesForLine
                     < height - count; numberOfSpacesForLine++)
             {
                if ()
                System.out.print(" ");
             }
             int numberToPrint = 1;
             for (int loopCount = 0; loopCount < lengthOfLine; loopCount++)
             {
                System.out.print(numberToPrint);
                if (loopCount < ((lengthOfLine - 1) / 2.0))
                {
                   numberToPrint *= 2;
                } else
                {
                   numberToPrint  /= 2;
                }
             }
             lengthOfLine += 2;
             System.out.println();
          }
       }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,764
    Rep Power
    7

    Default

    You can use String.format or the printf method.

  3. #3
    iriscience is offline Member
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    11
    Rep Power
    0

    Default

    It should be printf(%5d , numberToBePrinted); right?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,764
    Rep Power
    7

    Default

    Did you try it?

  5. #5
    iriscience is offline Member
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    11
    Rep Power
    0

    Default

    No, I haven't gotten a chance. I'm delivering pizzas and I had a chance to post my question real quick at the beginning of my shift. I did ask that badly. I meant *should* that work? If not, I don't expect you to just give me the answer. I was just hoping I'm going the right direction with it. Thanks so much for your help!

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,764
    Rep Power
    7

    Default

    Well it looks like it might be correct but the best way to find out is when you get on the computer and test if it does what you want.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You shouldn't be asking these questions if you're not able to first test them on your computer. We are no substitute for a Java compiler.

  8. #8
    iriscience is offline Member
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    11
    Rep Power
    0

    Default

    Junky, thank you so much for all your help and patience. I got some downtime and got it working. Thanks again!

Similar Threads

  1. Spacing/Alignment problem with java 2D printing
    By myselfmayur in forum Java 2D
    Replies: 2
    Last Post: 06-25-2010, 03:37 PM
  2. digit spacing
    By puk284 in forum New To Java
    Replies: 3
    Last Post: 06-09-2009, 02:49 PM
  3. Problem with margins/spacing
    By joseche in forum AWT / Swing
    Replies: 2
    Last Post: 12-26-2008, 08:22 PM
  4. Spacing buttons on a panel
    By 2o2 in forum New To Java
    Replies: 8
    Last Post: 10-20-2008, 10:44 AM
  5. Help with spacing in java
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •