I have already initialized all the values for months and date And i couldn't arrange all the dates in perfect column. So, can anyone help me This is my print code. for (int i = 0; i < 12; i++) { System.out.print(months[i]+" "); for (int j = 1; j <= 30; j++) { System.out.print(" " + date[j]); } System.out.println(); }
my question here is how to arrange the dates in perfect column for example like this: January 1 2 3 4 5 6 7 8... February 1 2 3 4 5 6 7 8... ...so on
This is in Java
At the end of your loop, after the days, add a new line. for (int i = 0; i < 12; i++) { System.out.print(months[i]+" "); for (int j = 1; j <= 30; j++) { System.out.print(" " + date[j]); } System.out.println("\n"); } This will print the month <days> <newline> month <days> <newline> etc.
This gives me : January 1 2 3... February 1 2 3... .... Here I don't have the dates on a straight line. Thus, the main problem is to print the dates in the following order January 1 2 3 4 5... February 1 2 3 4 5... See how the dates are lined up vertically [like all the 1s in one column 2s in column 3s in one column ]
Check out printf and format in your Java documentation. I just glanced at it, but if they're fully equivalent, using printf will help if your future includes C.
You are getting that because the month string is different lengths, this is a typical problem. If you do not wish to put a lot of effort into formatting then my suggestion would be to add a tab between them: System.out.print(months[i]+"\t");
Its much better with the tab but still some are way of like January 1 2 3 4 5 6 7 8 February 1...
Its perfect from march to august
Only the row: February, September, November, and December have problems
It seems to be when your string is over 7 characters. You could set them all to have two tabs, or you might consider putting a check that when the length is over 7 you add a second tab. In a console application you will find that formatting is difficult.
for (int i = 0; i < 12; i++) { if (months[i].length() > 7) { System.out.print(months[i]+"\t"); } else { System.out.print(months[i]+"\t\t"); } for (int j = 1; j <= 30; j++) { System.out.print(" " + date[j]); if (j == 15) System.out.print("\n\t\t"); } System.out.println(); }
oh its depending on the number of letters in the months. finally i got the output. Thank You
You're welcome
Join our real-time social learning platform and learn together with your friends!