Hi all, beginning programmer here. What I am trying to do is get a small amount of input from the user (day of the week the year starts on, and the year itself), and output a text grid calender like this:
JAN of 2011
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
There are three spaces between each date, and what I am trying to do is get the program to do is:
1) Begin a new line in the loop after the program reaches the seventh day <-- This has been achieved.
2) Print three spaces between each single digit <-- This has been achieved.
3) Print two spaces after each digit >= 10 .....which is where I'm stuck.
Here is my complete code:
import javax.swing.JOptionPane; public class Calender { public static void main(String[] args){ // Get year and start date from user String yearString = JOptionPane.showInputDialog("Enter current year:"); int year = Integer.parseInt(yearString); String startDateString = JOptionPane.showInputDialog( "Enter the day of the week for January 1:\n1 = Sunday \n2 = Monday \n3 = Tuesday" + "\n4 = Wednesday \n5 = Thursday \n6 = Friday \n7 = Saturday"); int startDate = Integer.parseInt(startDateString); // Create loop for 12 months for (int i = 1; i <= 12; i++){ String month = "xxx"; int days = 0; switch (i){ // Switch statement for diff. months case 1: month = "JAN"; days = 31; break; case 2:month = "FEB"; // FIX FOR LEAP YEAR EVENTUALLY! days = 28; break; case 3:month = "MAR"; days = 31; break; case 4:month = "APR"; days = 30; break; case 5:month = "MAY"; days = 31; break; case 6:month = "JUN"; days = 30; break; case 7:month = "JUL"; days = 31; break; case 8:month = "AUG"; days = 31; break; case 9:month = "SEP"; days = 30; break; case 10:month = "OCT"; days = 31; break; case 11:month = "NOV"; days = 30; break; case 12:month = "DEC"; days = 31; break; } System.out.println("\n" + month + " of " + year); System.out.println("S M T W T F S"); for (int j = 1; j < startDate; j++){ // Loop for formatting spacing for start date System.out.print(" "); } for (int j = 1, count = startDate; j <= days; j++, count++){ // Loop for outputting each day's number if (count % 7 == 0){ System.out.println(j); } /* if (count >= 10){ System.out.print(j + " "); } */ else System.out.print(j + " "); } } } }
The output I get is perfect (minus the formatting for double-digit dates), but when I apply the commented code is when the troubles start:
if (count >= 10){
System.out.print(j + " ");
}
The output produced from this repeats Saturday's date on the next line, and properly applies the two spaces between numbers 10 and 15, but not between the erroneous duplicate 15 and 16, 22 and 23, and 29 and 30. I am extremely confused, so any and all help/advice would be greatly appreciated! Thanks guys!