Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Etimer

Thread: 'if' statement not working?

  1. #1
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default 'if' statement not working?

    The 'if' statement part of the code isnt working.
    I want it to change the day to 1 and the month to the next month when the day equals 32 or 31 depending on how many days are in the month(32 and 31 referring to the day after the month ends).
    DD = day of the month
    MM = month
    dont worry about DAY and PUs.
    Also i wont need to go into the next year so dont worry about helping with that.

    Java Code:
    package packageA;
    
    public class d9 {
    	public static void main(String[]args){
    		int DD = 9;
    		int MM = 3;
    		int DAY = 1;
    		int PUs = 29;
    		int count = 1;
    		
    		System.out.println("MM/DD/YYYY, ###, #PU's\n----------------------");
    		while(count < 40){
    			System.out.printf("%02d/%02d/2012, %03d, %05d\n", MM, DD, DAY, PUs);
    			DD++; DAY++; PUs++;
    			if(MM == (1|3|5|7|8|10|12)){
    				if(DD == 32){
    					DD = 1;
    					MM += 1;
    				}
    			}else{
    				if(MM == (4|6|9|11)){
    					if(DD == 31){
    						DD = 1;
    						MM += 1;
    					}
    				}
    			}
    			count++;
    		}
    	}
    }
    THANKS!
    Last edited by Etimer; 03-10-2012 at 02:54 AM.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: 'if' statement not working?

    Clean up your code a bit and compartmentalise it so that it becomes easier to understand.

    I would use a switch statement instead of what you have done for the number of days in the month, and throw that into a separate method so that you can more clearly identify the problem.

    Java Code:
    private static final int MONTH_ERROR = -10;
    
    public int getLastDay(int month, int year) {
      switch (month) {
        case 1: //jan
        case 3: //mar
        case 5: //may
        case 7: //july
        case 8: //aug
        case 10: //oct
        case 12: //dec
            return 31;
        case 4: //apr
        case 6: //jun
        case 9: //sep
        case 11: //nov
            return 30;
        case 2: //feb
            return (isLeapYear(year)? 29 : 28);
        default:
            return MONTH_ERROR;
      }
    }
    
    public boolean isLeapYear(int year) {
      ...
    }
    Once you've done that you can use the method like this:
    Java Code:
    public boolean isLastDayOfMonth(int day, int month, int year) {
      int lastDay = getLastDay(month, year);
      if (day == lastDay) {
        return true;
      }
      return false;
    }
    And from that decide what to do:
    Java Code:
    int day = 10;
    int month = 3;
    int year = 2012;
    
    if (isLastDayOfMonth(day,month,year)) {
        //set to first day of next month
        day = 1;
        month++;
    }

  3. #3
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 'if' statement not working?

    THANK YOU VERY MUCH SIR!
    Although, i don't understand why what i used wouldnt work?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: 'if' statement not working?

    Quote Originally Posted by Etimer View Post
    THANK YOU VERY MUCH SIR!
    Although, i don't understand why what i used wouldnt work?
    Read this:
    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    and find the difference between the operator || and |.

  5. #5
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 'if' statement not working?

    Ahhh, ok. Thank you ozzyman, you've been a big help!

    Today has been a breakthrough in java for me!
    ozzyman likes this.

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  3. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •