6 Replies - 410 Views - Last Post: 07 October 2013 - 01:21 AM Rate Topic: -----

#1 chrisbklyn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 13

Day/month question. New to programming.

Posted 06 October 2013 - 03:19 PM

public static void main(String[] args)
{
	int InvoiceNum = 2000;
	int BalanceDue = 2000;
	int Day = 31;
	int Month = 9;
	int Year = 2013;
	int DayOne = 30;
	int DayTwo = 31;
	
	//Day/month Combos 
	
	if(Month==9||Month==4||Month==6||Month==11&&Day>=31)
	Day = DayOne;
	
	
	if(Day>=32&&Month==1||Month==2||Month==3||Month==5||Month==7||Month==8||Month==9||Month==10||Month==12)
	Day = DayTwo;
	
	
	// forcing numbers to 0
	
	if(InvoiceNum>1000)
		InvoiceNum = 0;
	else
		InvoiceNum = InvoiceNum;
	
	if(Month>12)
		Month = 0;
	else
		Month = Month;
	
	if(Year<2011||Year>2017)
		Year = 0;
	Year = Year;




Basically, my assignment is, for example, if someone puts Sept. 31st, to force it to sept. 30th. I'm not getting any errors of any sort, it just isn't forcing the numbers to what I want them to b. also, when I tried to make it something like "if(Day>32&&Month==1, 2, 3" etc. it doesn't work at all.

Sorry for the bad explanation of my problem, I don't know a lot of the terminology so I'm just trying to make it so it'll be somewhat understandable.

My code forcing them to zero if month is over 13, etc. all work. Which is why I don't understand why the Day/Month combo ones aren't working.

Is This A Good Question/Topic? 0
  • +

Replies To: Day/month question. New to programming.

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: Day/month question. New to programming.

Posted 06 October 2013 - 03:30 PM

if(Month==9||Month==4||Month==6||Month==11&&Day>=31)

This won't work as expected because and (&&) has higher precedence than or (||) so the condition Day>=31 will only be examined when the Month is 11.

if((Month==9||Month==4||Month==6||Month==11) && Day>=31)

The same issue applies with the following if-statement.
Was This Post Helpful? 2
  • +
  • -

#3 chrisbklyn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 13

Re: Day/month question. New to programming.

Posted 06 October 2013 - 03:46 PM

Ahhhhhh. Didn't know that the && would take priority over the ||. Thanks for the help, really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Day/month question. New to programming.

Posted 06 October 2013 - 03:49 PM

Quote

        //Day/month Combos 
        if ((Month == 9) || (Month == 4) || (Month == 6) ||
                ((Month == 11) && (Day >= 31))) {
            Day = DayOne;
        }


Nothing wrong with that code. What makes you think it doesn't work?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Day/month question. New to programming.

Posted 06 October 2013 - 04:52 PM

use an array

static int final nbDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: Day/month question. New to programming.

Posted 06 October 2013 - 04:57 PM

Post deleted.

This post has been edited by andrewsw: 06 October 2013 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Day/month question. New to programming.

Posted 07 October 2013 - 01:21 AM

Quote

use an array
Much better of course ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1