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 10 of 10

Thread: Help with formatting spacing on a calender grid please!

  1. #1
    Junior Member bulx0001's Avatar
    Join Date
    Jun 2011
    Location
    Minnesota
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Help with formatting spacing on a calender grid please!

    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!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with formatting spacing on a calender grid please!

    Use the printf method instead. It allows you to specify the width of a column. That is if you want to print "hello" in a column with a width of 15 it will insert the 10 spaces for you. For "Master" it will insert 9 spaces.

  3. The Following User Says Thank You to Junky For This Useful Post:

    bulx0001 (June 26th, 2011)

  4. #3
    Junior Member bulx0001's Avatar
    Join Date
    Jun 2011
    Location
    Minnesota
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Help with formatting spacing on a calender grid please!

    Thanks that helps a bunch with the column formatting issue. But now it messed up my initial start date, and now every month inserts a space in the entire first row and starts every month on sunday no matter what the user inputs.

    Here is the updated code that's still buggy

    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);		// Header for each month
    	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 k = 1, count = 0; k <= days; k++, count++){			// Loop for outputting each day's number
     
    				count += startDate;
     
    				if (count % 7 == 0){
    					System.out.println();
    				}
     
    				System.out.printf("%-4d", k);
    			}
     
     
    		}
     
    	}
     
    }

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with formatting spacing on a calender grid please!

    1. You never change the value of startDate which means each month will be printed starting on the same day.
    2. The order in which you print things matters.

  6. The Following User Says Thank You to Junky For This Useful Post:

    bulx0001 (June 26th, 2011)

  7. #5
    Junior Member bulx0001's Avatar
    Join Date
    Jun 2011
    Location
    Minnesota
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Help with formatting spacing on a calender grid please!

    Thanks man, I changed the order in which I printed the println statement and the formatting is now perfect. Your point #2 you made is spot on and I didn't notice it until just now... Thank you for that. I seem to be hitting a brick wall with regard to how to adjust the start date for each month based on the user only entering input for the starting day of the entire year once.

  8. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with formatting spacing on a calender grid please!

    Here's a hint:
    class Test {
        public static void main(String[] args) {
            int startDate = 6;
            int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
                    "Sep", "Oct", "Nov", "Dec"};
            String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
            for(int index = 0; index < months.length; index++) {
     
                System.out.println(months[index] + " begins on " + days[startDate]);
                startDate = (startDate + monthDays[index]) % 7;
            }
        }
    }

  9. The Following User Says Thank You to Junky For This Useful Post:

    bulx0001 (June 26th, 2011)

  10. #7
    Junior Member bulx0001's Avatar
    Join Date
    Jun 2011
    Location
    Minnesota
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Help with formatting spacing on a calender grid please!

    Thanks so much. I really appreciate all the help you've given me tonight, very kind of you.

    The thing is, I'm in an introductory Java class and we have not learned about arrays yet (that is what I assume the bracketed code is... from reading ahead a little in our textbood). Our instructor expected this program to be built without using arrays, I guess. Can this even be done without doing what you just did there?

  11. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with formatting spacing on a calender grid please!

    I wasn't suggesting you use arrays. The formula as the last line in the loop is what is important as it shows how to calculate the startDate of the next month. Think about how you can replace accessing the monthDays array with something already in your code.

  12. The Following User Says Thank You to Junky For This Useful Post:

    bulx0001 (June 27th, 2011)

  13. #9
    Junior Member bulx0001's Avatar
    Join Date
    Jun 2011
    Location
    Minnesota
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Help with formatting spacing on a calender grid please!

    Ah, gotcha! Very useful, thanks again man!!

    *EDIT: Nevermind, figured it out! Junky you are the freakin man!
    Last edited by bulx0001; June 27th, 2011 at 01:02 AM.

  14. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with formatting spacing on a calender grid please!

    Quote Originally Posted by bulx0001 View Post
    Does the formula you gave me assume Saturday = day 6...
    Yes it does since Saturday is in position 6 in the array. But I had to initialise startDate to be 6. Whereas you initialise it to be 7. Another problem is that using this formula when the start day is Saturday the value of startDate will be 0 as 35 % 7 == 0 and not 7. So either way you would have to do a bit of fiddling to get the exact startDate you want for each month.

Similar Threads

  1. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM
  2. HSSF cell date formatting in java
    By masepogurajesh in forum Java SE APIs
    Replies: 1
    Last Post: March 15th, 2011, 08:45 AM
  3. Grid GUI Library
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: September 15th, 2010, 03:30 PM
  4. Formatting output of a Double
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2010, 04:20 PM
  5. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM