Results 1 to 15 of 15
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default day number count

    what would be the best way to get the number day of the year? so far, i have:

    Java Code:
    		public static int dayCount(String curMonth, String curDay)
    		{
    		int day = 0;
    		int month = 0;
    		int monthNumbers = 0;
    		int dayNumber = 0;
    		
    		//We convert the day string to a integer
    		day = Integer.parseInt(curDay);
    		month = Integer.parseInt(curMonth);
    		
    		switch (month) 
       	  {
                case 1:  monthNumbers = 31;       break;
                case 2:  monthNumbers = 0;      break; // SPECIAL CASE!!
                case 3:  monthNumbers = 31;         break;
                case 4:  monthNumbers = 30;         break;
                case 5:  monthNumbers = 31;           break;
                case 6:  monthNumbers = 30;          break;
                case 7:  monthNumbers = 31;          break;
                case 8:  monthNumbers = 31;        break;
                case 9:  monthNumbers = 30;     break;
                case 10: monthNumbers = 31;       break;
                case 11: monthNumbers = 30;      break;
                case 12: monthNumbers = 31;      break;
                default: monthNumbers = 0; break;
            }
    		
    		dayNumber = day + ;

  2. #2
    YolanC is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    I suggest you use the DateTime class.

    You can make use the constructor:
    DateTime(int year, int monthOfYear, int dayOfMonth)

    Of this class you will need the "dayOfYear()" method.
    It will return the day of year in an integer if i'm not mistaken.

    and if you have to write the method yourself (for a school project eg.)
    I suggest you take a look at how the DateTime class works and use something similar

    Greets, Y
    Last edited by YolanC; 03-22-2011 at 08:20 PM. Reason: grammar and style flaws

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Quote Originally Posted by YolanC View Post
    I suggest you use the DateTime class.
    There is no such class in standard Java API. If you are refering to a third party lib then perhaps you should include a link to where they can get the jar file. Or maybe you mean the Date class.

  4. #4
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    yes, this is a school project :p lol.... i was thinking about a loop/counter combination...

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Do you have a specific question?
    Java Code:
    case 2:  monthNumbers = 0;      break; // SPECIAL CASE!!
    You can place an if statement inside a case statement to determine if the number of days should be 28 or 29.

    If this is to determine what day of the year it is (ie Jan 5 is the 5th day, Feb 1 is the 32nd day) then you can use the switch statement upside down without the break statements and add the number of days for that month to a running total. eg for april 19: add march(31), add february(28 or 29), add january(31) add 19 for april.

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    you would add those days in a month up, as long as the case number int is less than a certain number? and yes, i had to remind myself about feb., but thanks for the helpful tip on that one! :)

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Once again do you have a specific question?

  8. #8
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    hm, but if i took the breaks away, wouldn't it only add up the ones including, and after the month?

    e.x.: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    this uses arrays, which we haven't really learned about though....
    Last edited by droidus; 03-23-2011 at 11:16 AM.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ya, but you can determine the day from that number.

    If there are 200 days from current date to end of year, how would you determine how many days from the beginning of the year to the date?

  10. #10
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    ok, but i don't think i can even use that SwitchDemoFallThrough method, since we haven't even started arrays... somehow i have to get all of the past days in each month

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You don't need an array to use switch case fall through.

  12. #12
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    ok, so here is what i have so far... a work in progress yet...

    Java Code:
    		switch (month) 
       	  {
                case 12:  monthNumbers = 31;
                case 11:  monthNumbers = 30; 
                case 10:  monthNumbers = 31;
                case 9:  monthNumbers = 30;
                case 8:  monthNumbers = 31;
                case 7:  monthNumbers = 31;
                case 6:  monthNumbers = 31;
                case 5: monthNumbers = 30;
                case 4: monthNumbers = 31;
    				case 3: monthNumbers = 31;
    				case 2:                     // Gets????
    					if (leapYear == "true")
    						{
    							monthNumbers = 29;
    						}
    					else
    						{
    							monthNumbers = 28;
    						}
    				;
                case 1: monthNumbers = 31; break;
                default: break;
            }
    		  
    		 while (month < 12)
    		 	{
    		  		days = days + monthNumbers;
    				month++;
    			}

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your close, a few logic errors. When it falls through the switch case it shouldn't simply set monthNumbers to the amount of days in the month, it should add the number to the current value of month number

    Java Code:
    monthNumber += 30
    Like that.

    The next error, if leap year is a boolean, you can simply put it in a conditional like this
    Java Code:
    if(leapYear)
    If it's a string, you can't use == for comparison, instead you need to use a method in the String class, try checking the api.

  14. #14
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    Java Code:
    		// We convert the day string to a integer
    		day = Integer.parseInt(curDay);
    		month = Integer.parseInt(curMonth);
    		
    		 while (month <= 12 || month > 0)
    		 	{
    			counter ++;
    			
    				if (counter == 1)
    					{
    						month--;
    					}
    						
    				switch (month) 
    					{
                	case 12:  monthNumbers += 31;
                	case 11:  monthNumbers += 30; 
                	case 10:  monthNumbers += 31;
                	case 9:  monthNumbers += 30;
                	case 8:  monthNumbers += 31;
                	case 7:  monthNumbers += 31;
                	case 6:  monthNumbers += 31;
                	case 5: monthNumbers += 30;
                	case 4: monthNumbers += 31;
    					case 3: monthNumbers += 31;
    					case 2:
    						if (leapYear == "true")
    							{
    								monthNumbers += 29;
    							}
    						else
    							{
    								monthNumbers += 28;
    							}
    					;
                	case 1: monthNumbers += 31; break;
                	default: break;
    					}
    					
    		  		// Convert curDay from string to int type
    				day = Integer.parseInt(curDay);
    				
    		  		days = days + monthNumbers + day;
    				
    				month--;
    			}
    				
    			return days;
    it seems to run forever now... ( used the boolean == "" just for a temp quick fix [leapYear is a string, for now]... will fix later tho once i get this working, and thanks for the tip on that! :) )

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Firstly a boolean will never equal a String.

    Secondly there is no need to compare booleans. They are already true or false.
    Java Code:
    if(boolean)
    
    if(! boolean)
    Thirdly you do not need a case for 12. The only time you will add 31 days for December is if user enters 31st December as the date.

    Fourthly you need to switch on month - 1. Why? Because you want to add up all the days in the months before the entered month. Go and read the example I gave at the end of my previous post.

    Fifthly WTF are you using a loop?

Similar Threads

  1. how to count number of pages printed?
    By absmarty in forum New To Java
    Replies: 10
    Last Post: 01-31-2012, 06:20 PM
  2. Count number of digits in string using scanner
    By wendysbiggy in forum New To Java
    Replies: 35
    Last Post: 01-20-2010, 05:11 AM
  3. Array count number Occurances
    By gwithey in forum New To Java
    Replies: 2
    Last Post: 04-17-2009, 08:34 PM
  4. Replies: 4
    Last Post: 03-28-2009, 04:58 PM
  5. Replies: 8
    Last Post: 02-04-2009, 08:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •