Calendar from user input PROBLEM!
Hey guys, I am having trouble figuring out how to get my calendar program to function properly. It's part of a homework assignment and the problem asks for a java program using nested loops to print the calendar shown below. . Prompt the user to enter the name of the month, the number of days in the month, and the day of the week the month starts on. For example if the user enters: “February”, “29”, and “Wednesday”, print:
February
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
(Calendar doesn't print as it should on here but the 1st day should be under W as the user entered wednesday for their desired day for the month to start on, and the numbers should then correspondingly line up with the week days above. Sorry that they don't here :/ hopefully you can still see what it means.)
I haven't figured out the problem completely but what I have gotten so far for my code is below:
package calendar;
import java.util.Scanner;
/**
* This program asks the user for a month, the amount of days in the month, and
* the day of the week the month starts on, then generates a calendar for that
* month with the user entered data.
*/
public class Calendar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String month;
int daysInMonth;
String dayOfWeek;
String sunday = "Sunday";
String monday = "Monday";
String tuesday = "Tuesday";
String wednesday = "Wednesday";
String thursday = "Thursday";
String friday = "Friday";
String saturday = "Saturday";
System.out.print("Please enter the month for your calendar: ");
month = in.next();
System.out.print("Please enter the number of days in the month: ");
daysInMonth = in.nextInt();
System.out.print("Please enter the day of the week the month starts on: ");
dayOfWeek = in.next();
int daysPerWeek = 7;
int space;
space = daysPerWeek -1;
System.out.println(month);
System.out.println("--------------------");
System.out.println(" S M T W T F S ");
for (int numberOfWeeks = 1; numberOfWeeks <= 1; numberOfWeeks++){
for (int days = 1; days <= daysInMonth; days++){
if (days <= 9){
System.out.print(" ");
System.out.print(days);
}
if ((space + days) % 7 == 0){
System.out.println();
}
else{
System.out.print(" ");
}
}
}
}
}
So far when I run the program, It generates the calendar and the numbers will line up under the week days, but the days will not go past 9 and the starting day for the month does not start where the user enters.
Thank you in advance for any help I can get with this problem!!
(Sorry again my code doesn't show the indentations :S new to the forums)
Re: Calendar from user input PROBLEM!
**UPDATE** I have figured out the problem to the extent that I am only in need of help to get the user desired start day for the month to be the first day for the month!
Here is my updated code:
package calendar;
import java.util.Scanner;
/**
* This program asks the user for a month, the amount of days in the month, and
* the day of the week the month starts on, then generates a calendar for that
* month with the user entered data.
*/
public class Calendar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String month;
int daysInMonth;
String dayOfWeek;
String sunday = "Sunday";
String monday = "Monday";
String tuesday = "Tuesday";
String wednesday = "Wednesday";
String thursday = "Thursday";
String friday = "Friday";
String saturday = "Saturday";
System.out.print("Please enter the month for your calendar: ");
month = in.next();
System.out.print("Please enter the number of days in the month: ");
daysInMonth = in.nextInt();
System.out.print("Please enter the day of the week the month starts on: ");
dayOfWeek = in.next();
int daysPerWeek = 7;
int space;
space = daysPerWeek;
System.out.println(month);
System.out.println("S M T W T F S ");
System.out.println("--------------------");
for (int numberOfWeeks = 1; numberOfWeeks <= 1; numberOfWeeks++){
for (int days = 1; days <= daysInMonth; days++){
if (days <= 9){
System.out.print(days);
System.out.print(" ");
}else if (days > 9){
System.out.print(days);
System.out.print("");
}
if ((space + days) % 7 == 0){
System.out.println();
}
else{
System.out.print(" ");
}
}
}
}
}
Re: Calendar from user input PROBLEM!
Quote:
you wrote:
Code Java:
for (int days = 1; days <= daysInMonth; days++){
if (days <= 9){ // why is this here???????
System.out.print(" ");
System.out.print(days);
}
It wont go past 9 because you have a limiting if statement in your logic. Why is it there?
Re: Calendar from user input PROBLEM!
Yea i noticed that heh, It is for the spacing so that the single digit days spacing changes when you get into double digit days so that everything stays lined up with the weekday columns, in which in my updated code i added an else if (days > 9) to fix that. Thank you for the response :) still need help with getting the first day of the month to start where the user input states! (E.g. starts on monday, tuesday, wednesday, etc.)
Re: Calendar from user input PROBLEM!
*Updated Code*
Quote:
Java code
package calendar;
import java.util.Scanner;
/**
* This program asks the user for a month, the amount of days in the month, and
* the day of the week the month starts on, then generates a calendar for that
* month with the user entered data.
*/
public class Calendar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String month;
int daysInMonth;
String dayOfWeek;
String sunday = "Sunday";
String monday = "Monday";
String tuesday = "Tuesday";
String wednesday = "Wednesday";
String thursday = "Thursday";
String friday = "Friday";
String saturday = "Saturday";
System.out.print("Please enter the month for your calendar: ");
month = in.next();
System.out.print("Please enter the number of days in the month: ");
daysInMonth = in.nextInt();
System.out.print("Please enter the day of the week the month starts on: ");
dayOfWeek = in.next();
int daysPerWeek = 7;
int space;
space = daysPerWeek;
System.out.println(month);
System.out.println("S M T W T F S ");
System.out.println("--------------------");
for (int numberOfWeeks = 1; numberOfWeeks <= 1; numberOfWeeks++){
for (int days = 1; days <= daysInMonth; days++){
if (days <= 9){
System.out.print(days);
System.out.print(" ");
}else if (days > 9){
System.out.print(days);
System.out.print("");
}
if ((space + days) % 7 == 0){
System.out.println();
}
else{
System.out.print(" ");
}
}
}
}
}
Re: Calendar from user input PROBLEM!
On any given calendar there are 5 weeks given that 31/7 = 4.42...
A simple way to think of the calendar is that it's like an array: 5 rows, 7 columns;
Code Java:
int[][] dates = new int[5][7];
You should create an enumerated offset to identify which day of the week the month starts on:
Code Java:
public enum Day
{
SUNDAY(0),
MONDAY(1),
TUESDAY(2),
WEDNESDAY(3),
THURSDAY(4),
FRIDAY(5),
SATURDAY(6)
}
so when you begin to add things to your array, always add them with the offset
Code Java:
public static void addCalendarDatesToArray(int[][] calendarArray, int daysInMonth, int startsOn)
{
calendarArray = new int[5][7];
for(int i=0; i<daysInMonth; i++)
{
int day = i + startsOn;
int col = day % 7;
int row = day / 7;
calendarArray[row][col] = i;
}
}
Call the code like this:
Code Java:
int[][] dates = new int[5][7];
addCalendarDatesToArray(dates, 29, Day.WEDNESDAY);
You goal now is to take that array and format it to fit the calendar. Remember, a -1 value means there is no date for that array element.
Re: Calendar from user input PROBLEM!
Upload the finish product of it plss..
Re: Calendar from user input PROBLEM!
Guys, you are looking at this in a way too complicated fashion!
How's about this algorithm:
- Get month name, days in Month, and start day.
- Print the weekday headers (Sunday to Saturday) (American fashion ... :D)
- Pad the line with spaces equal to the start day offset.
- Use a loop to print each day of the month.
-- (if it has reached number of days in the week, start a newline)
Suggestion, since he nearly has this right:
Code :
if ((i + startDay) % 7 == 0)
System.out.println();