1 Attachment(s)
Java using printf to format.
Is it possible to print out the months like the picture below with that little space infront of January with printf? If I put "%10s" it will also make the months space far apart, I want before January to have a 10 blank space and between each months there 3 blank space. I try putting a "System.out.print(" ");" before the for loop but I'm trying to use printf . Thanks
Attachment 1676
Code :
String month[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
for(String monthOf : month)
{
System.out.printf("%6s",monthOf);
}
Re: Java using printf to format.
Can you build a line showing what you want to print?
Use *s for spaces.
Re: Java using printf to format.
**********S(Jan)***S(Feb)***S(Mar)***S(Apr)***S(Ma y)
^ Like that until Dec but disregard the ()
The picture should give a clearer explaination.
Re: Java using printf to format.
Print the first n spaces(FFFF) before the loop and then the desired spacing(SSS) in the loop
FFFFFSSSJanSSSFeb...
Re: Java using printf to format.
Something like this?
Code :
System.out.print(" ");
for(String monthOf : month)
{
System.out.printf("%6s",monthOf);
}
Re: Java using printf to format.
Did you try it? Did it give you what you want?
Re: Java using printf to format.
Yes, I got it now. Thank you for your help! Happy New Year :D