Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Calendar from user input PROBLEM!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation 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)
    Last edited by Exiled; February 23rd, 2012 at 03:43 PM. Reason: Calendar format issue/ Code format issue


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  3. #3

    Default Re: Calendar from user input PROBLEM!

    you wrote:
    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?
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.)

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Calendar from user input PROBLEM!

    *Updated Code*
    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(" ");
    }
    }
    }
    }
    }

  6. #6

    Default 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;
    int[][] dates = new int[5][7];

    You should create an enumerated offset to identify which day of the week the month starts on:
    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
    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:

    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.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calendar from user input PROBLEM!

    Upload the finish product of it plss..

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default 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 ... )
    • 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:
    if ((i + startDay) % 7 == 0)
    System.out.println();

Similar Threads

  1. Need help with user input/output for GUI.
    By Tctwins in forum AWT / Java Swing
    Replies: 6
    Last Post: February 20th, 2012, 04:13 PM
  2. user input mismatch problem
    By JavaN00b101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2012, 07:02 AM
  3. Action Listener?user input problem
    By gpelefty90 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 2nd, 2011, 05:17 AM
  4. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM

Tags for this Thread