Nikki Fairley
Nikki Fairley

Reputation: 3

Hi im trying to have my if statement equal to more then one number

I know this is incorrect and I cant figure it out. Do I just need to change my whole method? This website also said that this have question has been ask, but none of them help me at all.

public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear){
        if (cMonth = 01 && 12){
            month = cMonth;
            if (cMonth = 01,03,05,07,08,10,12){
            if (cDate <= 31 ){
            date = cDate;
            }// end of if
            }// end of if(cMonth) months with 31 days
        else if(cMonth = 04, 06, 09, 11){
            if (cDate <=30){
                date = cDate;
            }
}// end of cMonth month within 30 days

Upvotes: 0

Views: 58

Answers (5)

Mukesh Verma
Mukesh Verma

Reputation: 534

    Set<Integer> set1 = new HashSet<Integer>(new Integer[]{1,3,5,7,8,10,12});
    Set<Integer> set2 = new HashSet<Integer>(new Integer[]{4,6,9,11});
    if(cMonth == 1 || cMonth == 12){
        if(set1.contains(cMonth)){
            if (cDate <= 31 ){
                date = cDate;
            }
        }
        else if(set2.contains(cMonth)){
            if (cDate <= 30 ){
                date = cDate;
            }
        }
    }

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074218

There are a few issues:

  • Comparison is done with ==, not =. = is assignment.

  • Numbers with leading 0s like 01 and 02 and such are octal. You probably wanted 1, 2, and such which are decimal.

  • You're expecting && to know what you want to compare 12 with, but && doesn't work that way. Both operands to && need to specify what you're comparing.

  • && means "AND". If you want to follow a branch if cMonth is 1 or if it's 12, you want || ("OR").

  • cMonth = 04, 06, 09, 11 also needs you to be explicit about what you want to do with 06, 09, and 11.

So your outermost if needs to be:

public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear){
    if (cMonth == 1 || cMonth == 12){
        // ...
    }
    else if(cMonth == 4 || cMonth == 6 || cMonth == 9 || cMonth == 11) {
        // ...
    }
    // ...
}

I'll leave the inside of the branches to you, applying the list of notes above.

Separately: You might look at using a switch statement instead of all of those cMonth == comparisons.

public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear){
    switch (cMonth) {
        case 1:
        case 12:
            // ...
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            // ...
            break;
    }
    // ...
}

Upvotes: 0

JnB
JnB

Reputation: 38

if (cMonth = 01 && 12)

For this to be true the month must be 1 and 12, I don't think that's possible. I guess you want to check if the month is between 1 and 12, so it should be

if (cMonth >= 1 && cMonth <= 12)

Also this:

if (cMonth = 01,03,05,07,08,10,12){

won't work, you should try a switch or simply this (very much typing):

if(cMonth == 1 || cMonth == 3 || cMonth == 5 || cMonth == 7 || cMonth == 8 || cMonth == 10 || cMonth == 12)

Upvotes: 0

flyingfox
flyingfox

Reputation: 13506

You can use List to do it

List<Integer> monthList = new ArrayList<Integer>();
monthList.add(4);
monthList.add(6);
monthList.add(9);
monthList.add(11);
if(monthList.contains(cMonth)){
   //do your work
}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533492

A switch statement is probably best for this.

public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear) {
    switch (cMonth) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if (cDate > 31)
                throw new IllegalArgumentException();
            break;

        case 4:
        case 6:
        case 9:
        case 11:
            if (cDate > 30)
                throw new IllegalArgumentException();
            break;
        case 2:
            int days = isLeapYear(cYear) ? 29 : 28;
            if (cDate > days)
                throw new IllegalArgumentException();
            break;
        default:
            throw new IllegalArgumentException();
    }
    date = cDate;
}

Something closer to what you asked for, but something far, far less efficient is

public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear) {
    if (Arrays.asList(1, 3, 5, 7, 8, 10, 12).contains(cMonth)) {
        if (cDate > 31)
            throw new IllegalArgumentException();

    } else if (Arrays.asList(4, 6, 9, 11).contains(cMonth)) {
        if (cDate > 30)
            throw new IllegalArgumentException();

    } else if (cMonth == 2) {
        int days = isLeapYear(cYear) ? 29 : 28;
        if (cDate > days)
            throw new IllegalArgumentException();
    } else {
            throw new IllegalArgumentException();
    }
    date = cDate;
}

btw numbers which start with 0 are in octal so 08 and 09 are not valid.

Upvotes: 1

Related Questions