Reputation: 1
import javax.swing.JOptionPane;
public class Calendar31 {
static String strDays = " %2d ";
public static void main(String[] args) {
String getYear = JOptionPane.showInputDialog(null, "Enter Year to Show: ");
int year = Integer.parseInt(getYear);
String startingDay = JOptionPane.showInputDialog(null, "Enter Starting Day (0 = Sunday, 6 = Saturday): ");
int startDay = Integer.parseInt(startingDay);
String[] months = new String[12];
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";
for (int y = 0; y <= 11; y++) {
System.out.println(" " + months[y] + " " + year);
System.out.println("---------------------------------");
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for (int x = 0; x < startDay; x++){
System.out.print(" ");
}
for (int d = 1; d <= getNumDays(1, year); d++){
System.out.printf(strDays, d);
if ((d + startDay) % 7 == 0){
System.out.println();
}
}
System.out.println();
System.out.println();
}
}
public static boolean isLeapYear(int year){
return ((year % 4 == 0)&& (year % 100 != 0)) || (year % 400 == 0);
}
public static int getNumDays(int m, int year) {
int[] numDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int retval = numDays[m-1];
if ((m == 2) && (isLeapYear(year))){
retval++;
}
return retval;
}
}
So, I have this code. The project is to make a java program that displays the yearly calendar depending on the year you input and the starting day of the first month... made another method to determine leap year because some idiot a long time ago decided we need that extra 1/4 day in a year... so that does work when the month is the second month. In that method, there's the array with 12 entries, those 12 being Jan, Feb, Mar, so on. Now, in the main block of code up top there is the line :
for (int d = 1; d <= getNumDays(1, year); d++) {
where the 1 is, that pulls the first int from the array in getNumDays... for some reason, I cannot figure out where I went wrong, but the 1 needs to go away and in its place we need 'm', as defined in getNumDays. But no matter what I cannot figure out the syntax needed for this... I'm sure its something simple. Any quick help out there? Thanks!
Upvotes: 0
Views: 74
Reputation: 101
Maybe I'm missing something, but couldn't you just change the "1" you reference to "y"? As in:
for (int d = 1; d <= getNumDays(y, year); d++){
After all, the outer loop counts from 0 to 11 and assigns the value to y. Then, you can just use that value to call the getNumDays function and return the proper number of days in month "y".
Also, you may want to consider using tabs instead of a predefined number of spaces. This will ensure that your columns line up. For example:
System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
for (int x = 0; x < startDay; x++){
System.out.print("\t");
}
Upvotes: 1