d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help With Printing A Calendar Month
Add Reply New Topic New Poll
Member
Posts: 1,863
Joined: Jul 14 2007
Gold: 3,601.83
Nov 2 2013 05:56pm
writing a program where it asks for the month 1-12, 1 being january and etc, then the day it starts on, 1-7. 1 is sunday, 2 is monday, etc.

side note : Keyboard is a class i have included in my project.

it works for the most part. my only problem now is when i have the month start on a date other than 1 (sunday) the output messed up and i cant figure out why.

when i make it start on sunday, this is how it prints:


and this happened when i tried to print it on the "third" day, this is what happens:


heres my code:
Code


import java.io.*;

public class CalendarMonth
{
static final int WEEK = 7;

public static void main(String[] args) throws IOException
{
String proceed;
char repeatCheck;
int days;
String leapYear;
char leapYearCheck;
int startDay;
String monthName;

do
{
days = getDays();


System.out.println(days);


System.out.println("Please enter a number 1-7 that the month starts on: (1 for sunday, 2 for monday, etc)");
startDay = Keyboard.readInt();


while(startDay < 1 || startDay > 7)
{
System.out.println("You did not enter a number 1-7, Try again: ");
startDay = Keyboard.readInt();
}

System.out.println(" S M T W TH F Sa");

printMonth(days,startDay);


System.out.println("\nWould you like to print another month?");
proceed = Keyboard.readString();


repeatCheck = proceed.charAt(0);


}while(repeatCheck == 'y');
}






public static int getDays() throws IOException
{
int month;
String leapYear;
int startDay;
int days = 0;
char leapYearCheck;

System.out.println("Please input a number 1-12 to print the corresponding month: ");
month = Keyboard.readInt();
while (month < 1 || month > 12)
{
System.out.println("You did not put a number 1-12, Try again: ");
month = Keyboard.readInt();
}

switch(month)
{
case 1: days = 31;
break;
case 2:

System.out.println("is it a leap year? ");
leapYear = Keyboard.readString();
leapYearCheck = leapYear.charAt(0);

while(leapYearCheck != 'y' && leapYearCheck != 'n')
{
System.out.println("you did not enter a yes or no answer, is it a leap year?");
leapYear = Keyboard.readString();
leapYearCheck = leapYear.charAt(0);
}

if (leapYearCheck == 'y')
{
days= 29;
}
else
{
days = 28;
}

break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 31;
break;
case 6: days = 30;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 31;
break;
case 11: days = 30;
break;
case 12: days = 31;
break;
}

return days;
}





public static void printMonth(int days, int startDay)
{
int count;

count = startDay;
for (int counter = 1; counter <= 7; counter++)
{
if (counter < startDay)
{
System.out.print(" ");
count = count-1;
}
else
{
System.out.printf("%3d", count);

count++;
}
}


System.out.println();
for(int restOfTheMonth = count; restOfTheMonth <= days; restOfTheMonth++)
{

System.out.printf("%3d", restOfTheMonth);
if (restOfTheMonth%WEEK==0)
{
System.out.println();
}
}



}


}

Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Nov 2 2013 11:44pm
retract

This post was edited by eagl3s1ght on Nov 3 2013 12:14am
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Nov 3 2013 12:53am
Here it is in Javascript, you can test to see how it looks here: http://jsfiddle.net/8YQ7p/

I did it very differently, and I think you'll do best in following my lead. The problem with how you've done it is because you do week by week instead of day by day, your loop won't know to continue the dates on your next week.
Another small problem with your code is that you only add two spaces of padding, you are forgetting the space between your weekdays. For the first date (when you're skipping days) you should use
Code
" "
not
Code
" "



Code:
Code
var month_total_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
starting_day = 3; // wednesday
current_day = starting_day; // first current day is determined by starting day
var week_str = ''; // we print whole weeks at a time

// print calendar header
$('pre').append('<strong>' + ' Su Mo Tu We Th Fr Sa' + '</strong>' + '\n');

for(day = 1; day <= month_total_days[1]; day++){

// rise and shine
current_day++;

// on the first day, we apply the padding for the days we're skipping
if(day == 1){
for(i = 1; i <= starting_day; i++){
week_str += ' ' + ' '; // 1 weekday margin + 2 padding because integers will be < 10
}
}

// [ 1] vs [23]: 1 weekday margin + 1 padding is needed for 1 char length
week_str += (day < 10) ? ' ' : ' ';

// print the date
week_str += day;

// on the last day of the week, we print every date gathered into our week_str/ing
if(current_day == 7){
$('pre').append(week_str + '\n');
week_str = '';
current_day = 0;
} else if(day == month_total_days[1]){
$('pre').append(week_str + '\n');
}
}


This post was edited by eagl3s1ght on Nov 3 2013 12:54am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll