Reputation: 679
I have seen that one can use multiple ternary conditions, but haven't found a way to assign two variables if a single condition is true. This is the method I'm trying to write:
int[] chkNext(int mnd, int y) {
int[] date = new int[2];
mnd = 12 ? mnd = 1, y++ : mnd++; // returns the following: "error: : expected"
date[0] = mnd, date[1] = y;
return date;
}
Upvotes: 0
Views: 4548
Reputation: 1
The construct of ternary operator is wrong as there should be condition before '?'. As Mat suggested better option is using if statement.
Ternary can be generally used for simple statements like
boolean isEven = (n!=0 && n%2 == 0) ? true : false;
Upvotes: -3
Reputation: 206689
Just use an if
statement.
if (mnd == 12) {
// ^^ very important
mnd = 1;
y++;
} else {
mnd++;
}
And this:
date[0] = mnd, date[1] = y;
Would be better as:
date[0] = mnd; date[1] = y;
Don't use a comma operator if you don't really need it.
Upvotes: 8