0

I'm going to try my best to word my question clearly, I apologize if I do not make sense in my question, I am still very new to java (several weeks in to my first Java class).

Part of my assignment is: "Modify the constructor in the Invoice class, so the day is not greater than 31, 30, or 28, depending on the month. Also if the month is invalid, and thus forced to 0, also force the day to 0."

So in my original program I have this as my month and day methods:

public void setMonth(int month) {
if(month < 1 || month > 12) //if month is less than 1 and greater than 12 force number to 0
    this.month = 0;
else
    this.month = month;
}

public void setDay(int day) {
if(day < 1 || day > 31) //if day is less than 1 and greater than 31 force to 0
    this.day = 0;
else
    this.day = day;
}

So I guess where my confusion is, is should I use an 'else if' to make sure the day is not greater than 31, 30, or 28 depending on the month? (also I am not sure if 'else if' is supported in Java? C++ is what I know the most about)

Like:

else if(month = 2 && day > 28)
this.day = 0;

(I know this code is incorrect, it had an error to it when I was typing it). What do you think would be the best way for me to go about making sure the day is not greater than 31, 30, or 28 depending on the month?

yuki
  • 43
  • 5
  • If else if is supported in Java just like C++ does. However, be careful that `if (1)` doesn't work – Sweeper Oct 20 '15 at 05:31
  • You can use else if, but it will not be efficient. Use java Calendar instead and work with this. – m.aibin Oct 20 '15 at 05:32
  • 1. else if is supported by java. 2. In the code you shared: else if(month = 2 && day >28), month is getting assigned a value, be careful of this programming mistake which we face in general, because here, you are trying to do a comparison and not an assignment. – a3.14_Infinity Oct 20 '15 at 05:32
  • You could create an array of 12 elements that has the number of days in each month. That way you'd avoid a few `if`s. (I'm assuming you don't care about leap years.) – ajb Oct 20 '15 at 05:33
  • You can check if passed date is valid using `Calendar` and `Date` object. Check http://stackoverflow.com/a/226920/223386 – hsz Oct 20 '15 at 05:33
  • else if(month == 2 && day > 28) {this.day = 0;} – Shailesh Yadav Oct 20 '15 at 05:36

3 Answers3

4
else if(month = 2 && day > 28)
this.day = 0;

(I know this code is incorrect, it had an error to it when I was typing it). What do you think would be the best way for me to go about making sure the day is not greater than 31, 30, or 28 depending on the month?

The error is NOT in the else if part, but in the month = 2 part. You are actually assigning 2 to month instead of comparing it.

use month == 2 and your code will just work fine.

Codebender
  • 14,221
  • 7
  • 48
  • 85
0

also I am not sure if 'else if' is supported in Java?

It is very much supported in Java as well.

if(day < 1 || day > 31) 
    this.day = 0;
else
    this.day = day;
}

If condition for day < 1 || day >31 cover's up everything, no need for else if, but you might need to consider for 30 days month as per your Business logic

else if(month = 2 && day > 28)
this.day = 0;

= is for assignment, and == is for comparison, Need to compare using ==,

 else if(month == 2 && day > 28)
    this.day = 0;
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

Thank you so much everyone! I feel really silly knowing i didn't put two equal signs in that code! Sorry for such a simple mistake!

else if (month == 2 && day > 28)
    this.day = 0;
else if (month == 4 || month == 6 || month == 9 ||  month == 11 && day > 30)
    this.day = 0;

This is what I ended up doing and everything worked perfectly! Thanks so much!!

yuki
  • 43
  • 5