# Thread: How to right align numbers?

1. Member
Join Date
Mar 2013
Posts
18
Rep Power
0

## 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:

2. ## Re: How to right align numbers?

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

kind regards,

Jos

3. ## 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?

4. ## Re: How to right align numbers?

Originally Posted by makpandian
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

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

6. ## Re: How to right align numbers?

Come on, Jos. You and your modern stuff. Jeez, using an API. What next? ;-)

7. ## Re: How to right align numbers?

Originally Posted by PhHein
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

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

## 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. ## Re: How to right align numbers?

Sure, what did you think?

10. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: How to right align numbers?

Originally Posted by LaughingSeraph
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...

11. ## Re: How to right align numbers?

Originally Posted by Tolls
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...
It is spelled 'kool', with a 'k'.

kindest regards,

Jos ;-)

12. ## Re: How to right align numbers?

You've quoted the wrong post, old man.

13. ## Re: How to right align numbers?

Originally Posted by PhHein
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

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

## 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. Member
Join Date
Mar 2013
Posts
18
Rep Power
0

## Re: How to right align numbers?

thank you:) it works!

16. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: How to right align numbers?

Originally Posted by LaughingSeraph
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.

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

## 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.

#### Posting Permissions

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