Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (myname):

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(); }

OpenStudy (myname):

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

OpenStudy (myname):

This is in Java

OpenStudy (espex):

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.

OpenStudy (myname):

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 ]

OpenStudy (rsmith6559):

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.

OpenStudy (espex):

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");

OpenStudy (myname):

Its much better with the tab but still some are way of like January 1 2 3 4 5 6 7 8 February 1...

OpenStudy (myname):

Its perfect from march to august

OpenStudy (myname):

Only the row: February, September, November, and December have problems

OpenStudy (espex):

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.

OpenStudy (espex):

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(); }

OpenStudy (myname):

oh its depending on the number of letters in the months. finally i got the output. Thank You

OpenStudy (espex):

You're welcome

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!