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

Thread: Newbie Java Question

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Newbie Java Question

    Please help me out.
    I understand most everything in this program,
    but there is one bit that's driving me crazy.

    Let's say my inputs are year=1800, month=1
    On line 80 ---> return (totalNumberOfDays + startDay1800) % 7;
    which means (31 + 3) % 7 = 6

    On line 57 ---> for (i = 0; i < startDay; i++)
    so doesn't this mean for (i=0; i<6; i++)

    How can the remainder be 6. I must be reading it wrong but how.

    import javax.swing.JOptionPane;
     
    public class PrintCalendar
    {
    	public static void main (String[]args)
    	{
     
    		String strMonth = JOptionPane.showInputDialog(null,"Please enter the month","MONTH",JOptionPane.QUESTION_MESSAGE);
    		int month = Integer.parseInt(strMonth);
     
    		String strYear = JOptionPane.showInputDialog(null,"Please enter a year","YEAR",JOptionPane.QUESTION_MESSAGE);
    		int year = Integer.parseInt(strYear);
     
    		printMonth(year,month);
    	}
    	//Run - print Title and Body
    	static void printMonth(int year, int month)
    	{
    		printMonthTitle(year, month);
     
    		printMonthBody (year, month);
    	}
    	//Run - Title Print
    	static void printMonthTitle(int year, int month)
    	{
    	System.out.println("      " + getMonthName(month) + year);
    	System.out.println("----------------------------");
    	System.out.println(" Sun Mon Tue Wed Thu Fri Sat");	
    	}
    	//Validate - Month String
    	static String getMonthName(int month)
    	{
    		String monthName = null;
    		switch (month){
    			case 1: monthName = "January"; break;
    			case 2: monthName = "February"; break;
    			case 3: monthName = "March"; break;
    			case 4: monthName = "April"; break;
    			case 5: monthName = "May"; break;
    			case 6: monthName = "June"; break;
    			case 7: monthName = "July"; break;
    			case 8: monthName = "Ausgust"; break;
    			case 9: monthName = "September"; break;
    			case 10: monthName = "October"; break;
    			case 11: monthName = "November"; break;
    			case 12: monthName = "December"; break;
    		}
    		return monthName;
    	}
    	//Run - Body Print 
    	static void printMonthBody(int year,int month)
    	{
    		int startDay = getStartDay(year, month);
    		int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
     
     
    		for(int i=0; i<startDay; i++)
    		{
    			System.out.print("    ");
    		}
    		for(int i=1; i<=getNumberOfDaysInMonth(year, month); i++)
    		{
    			if (i<10)
    				System.out.print("   " + i);
    			else 
    				System.out.print("  " + i);
     
    			if ((i + startDay)%7 == 0)
    				System.out.println();
    		}
    	}
    	//Validate - which day the date starts on.
    	static int getStartDay(int year, int month)
    	{
    		//On the 1/1/1800 starts on a Wednesday 3 days away from Sunday
    		int startDay1800 = 3;
    		int totalNumberOfDays = getTotalNumberOfDays(year, month);
     
    		//if the total "number of days + startDay1800 in this case is 3 days" is divisible by seven
    		return (totalNumberOfDays + startDay1800)%7;
    	}
    	//Validate - the total number of days in the year and month
    	static int getTotalNumberOfDays(int year, int month)
    	{
    		int totalDays = 0;
    		//Add 366 or 365 days if it is more then 1800 year
    		for(int i=1800; i<year; i++)
    			if (isLeapYear(i))
    				totalDays = totalDays + 366;
    			else
    				totalDays = totalDays + 365;
     
    		//add the days in as each month rows by
    		for(int i=1; i<month; i++)
    			totalDays = totalDays + getNumberOfDaysInMonth(year, i);
     
    		return totalDays;
    	}
    	//Validate - the days for each of the month
    	static int getNumberOfDaysInMonth(int year, int month)
    	{
    		if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
    			return 31;
    		if(month==4 || month==6 || month==9 || month==11)
    			return 30;
    		if(month==2)
    			return isLeapYear(year)? 29:28;
    		else 
    			return 0;
    	}
    	//Testing - Leap Year ? True or False
    	static boolean isLeapYear(int year)
    	{
    		return year % 400 == 0 || (year % 4 == 0 && year % 100 !=0);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Newbie Java Question

    Can you add some println statements to output data that shows the problem?
    Add some comments to the output and post it here.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,181
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Newbie Java Question

    I have moved this post from the Member Introductions forum to this one for better topic alignment.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Newbie Java Question

    i am a noob to Java, though a programmer for 25 years, but...

    (i = 0; i < startDay; i++)
    so doesn't this mean for (i=0; i<6; i++)

    if the for next loop is going to stop when i is not less than 6 (I<6) and i = 6 it does not satisfy the condition to go through the loop. you can change this to i <= 6.
    Buddy

Similar Threads

  1. Addition of doubles (newbie question)
    By archyb in forum Java Theory & Questions
    Replies: 2
    Last Post: April 21st, 2011, 09:52 AM
  2. Newbie Question on a Code - I don't know if this is Java
    By fresca in forum Java Theory & Questions
    Replies: 4
    Last Post: April 7th, 2011, 08:39 PM
  3. hello~ i am newbie here and to java
    By puyunk in forum Member Introductions
    Replies: 1
    Last Post: March 4th, 2011, 05:57 AM
  4. newbie question about Abstract methods
    By FailMouse in forum Java Theory & Questions
    Replies: 3
    Last Post: August 10th, 2010, 11:51 PM
  5. Looping Question (newbie)
    By xdrechsler in forum Loops & Control Statements
    Replies: 13
    Last Post: July 19th, 2010, 09:12 PM