Results 1 to 17 of 17
  1. #1
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default How to right align numbers?

    Everything is working perfect and the output is what i need, except that the single digit numbers don't align with the 2 digit numbers. how can i right align them? someone help!?
    Java Code:
    //
    // problem # 6 - table of numbers and their squares
    //
    public class NumberOfSquare{
    public static void main(String args[]) {
       System.out.println ("\nThis is problem # 6\n");
    
       System.out.print("\tnum " + "square " + "\tnum " + "square "
                + "\tnum " + "square " + "\tnum " + "square \n");
          
          for(int g=0;g<20;g++)
    {
    for(int i=g+1;i<=80;i=i+20)
    {
    System.out.print("\t" + i+"   "+(i*i)+"   ");
    }
    System.out.println();
    }
    }
    }
    My output prints this:
    How to right align numbers?-screen-shot-2013-03-12-11.42.36-am.png

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to right align numbers?

    Check the System.out.printf( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default Re: How to right align numbers?

    DecimalFormat df=new DecimalFormat("#0000");
    DecimalFormat df1=new DecimalFormat("#00");


    System.out.println ("\nThis is problem # 6\n");

    System.out.print("\tnum " + "square " + "\tnum " + "square "
    + "\tnum " + "square " + "\tnum " + "square \n");

    for(int g=0;g<20;g++)
    {
    for(int i=g+1;i<=80;i=i+20)
    {
    System.out.print("\t" + df.format(i)+" "+df.format(i*i)+" ");
    }
    System.out.println();
    }
    Can you please try this and let me know its fine?
    Mak
    (Living @ Virtual World)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to right align numbers?

    Quote Originally Posted by makpandian View Post
    Can you please try this and let me know its fine?
    I wouldn't call it 'fine', rather a bit clumsy; see what the System.out.printf( ... ) method can do for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How to right align numbers?

    Come on, Jos. You and your modern stuff. Jeez, using an API. What next? ;-)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to right align numbers?

    Quote Originally Posted by PhHein View Post
    Come on, Jos. You and your modern stuff. Jeez, using an API. What next? ;-)
    I didn't mention an API (in this thread), besides, reading makes your eyeballs hurt; programming is supposed to be blindly copying fragments of code you don't understand from teh internet, keeping your fingers crossed or applying a bit of voodoo now and then. It is science after all ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: How to right align numbers?

    Awesome, I've found the cool kids section. Does this mean we can all be snide and nasty if we have more experience than someone else?

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How to right align numbers?

    Sure, what did you think?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default Re: How to right align numbers?

    Quote Originally Posted by LaughingSeraph View Post
    Awesome, I've found the cool kids section. Does this mean we can all be snide and nasty if we have more experience than someone else?
    I'm curious...what about the above prompted this?
    It can hardly be Jos' response to makpandian's post, as mak has been around here for years...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to right align numbers?

    Quote Originally Posted by Tolls View Post
    I'm curious...what about the above prompted this?
    It can hardly be Jos' response to makpandian's post, as mak has been around here for years...
    <Pink Panther tune fades in/>
    It is spelled 'kool', with a 'k'.
    <Pink Panther tune fades out/>

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How to right align numbers?

    You've quoted the wrong post, old man.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to right align numbers?

    Quote Originally Posted by PhHein View Post
    You've quoted the wrong post, old man.
    No I haven't; that was just so kewl ... and who is the old man here?

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: How to right align numbers?

    Masters level in psychology means I could easily post a bunch of stuff about how unhelpful a RTFM post is, or a post that belittles or attempts to make the OP feel stupid because they didn't know how to find information. The odds are, that most people wouldn't even be able to dig up the appropriate information to really understand what was being posted.

    Knowing to google for "JAVA API FORMAT STRING OUTPUT CONSOLE", might be a bit much for a new person. Given what she provided to start with, she probably did read the API, or at least to the best of her ability. Dude then gave a great answer, no arguing that at all. He pointed her to the function that could best solves her problem, and did so like a good teacher would.

    The part about the API, and voodoo and all of that, easily falls into something that could be good old fashion in group solidification by marginalizing out group individuals. This makes the recipient feel inferior, and prone to egress. That quickly leads to ideological endogamy. That is bad because, simply put, Java is itself the product of people refusing to fall into line with existing conventions and patterns of thought.

    I was actually taken aback at how how mean spirited it ended up sounding. In fact, I literally doubled checked to make sure I hadn't stumbled into a linux forum.

    I understand it is important for people to look for solutions on their own, avoid hand holding, and put forth a best effort to solve problems autonomously etc. etc.
    I truly understand how irksome it can be when people don't get it, or they want more help than they need.
    Trashing them like they were stupid for not knowing what you know, well, that really doesn't do much good.

    Things may seem obvious to you. But to a new person, the API is gigantic, intimidating and can be a pain to find what one actually needs. Plus, we were all new and useless at programming at some point. Some of us were able to get by splendidly in a classroom, some of us with books, some of needed someone to hold their finger out and point in the right direction. That's not voodoo, that is actually the ineffable science of learning and memory, social economics etc.

  15. #15
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: How to right align numbers?

    thank you:) it works!

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default Re: How to right align numbers?

    Quote Originally Posted by LaughingSeraph View Post
    The part about the API, and voodoo and all of that, easily falls into something that could be good old fashion in group solidification by marginalizing out group individuals.
    Well, the rest might actually be relevant, except this is at the crux of it and so I'll skip the other parts since this is misunderstanding Jos' post.
    This being the internet, of course, that's always a strong possibility.
    He wasn't having a poke at the OP in the slightest since, as should be quite obvious, the OP hadn't even had a chance to come back to respond (til just now). It was a general despair about what we see here all too often. And one of the reasons we're not fans of posting solutions. RTFM is an art that is really (honestly) a key part of this whole programming lark.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: How to right align numbers?

    If I misunderstood it, and it wasn't an attack or snideness then I stand corrected and apologize.
    I do truly understand the frustration that can occur.

Similar Threads

  1. Component align
    By tnrh1 in forum AWT / Swing
    Replies: 6
    Last Post: 05-23-2012, 06:43 AM
  2. How to align in java
    By deemu in forum New To Java
    Replies: 2
    Last Post: 02-06-2012, 08:50 PM
  3. Align Row to Right in JTable
    By shomid in forum AWT / Swing
    Replies: 2
    Last Post: 11-25-2011, 10:23 PM
  4. Align left
    By britto_bicsjohn in forum AWT / Swing
    Replies: 2
    Last Post: 09-09-2009, 04:05 AM
  5. How to align the output on console?
    By sfe23 in forum New To Java
    Replies: 5
    Last Post: 03-30-2009, 03:28 AM

Posting Permissions

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