Suzannah Thomas
Suzannah Thomas

Reputation: 29

Is there a || operator for int?

I am trying to write a method that takes a single int parameter that is the numeric month and returns the number of days in the given month. So, a parameter of 1 would return 31 since there are 31 days in January and a parameter of 2 would return 28 since there are 28 days in February.

Here's what I have so far:

 public static void daysInMonth(int month) {
           if (month == 1||3||5||7||8||10||12)
         System.out.println("31");
      else if (month == 4||6||9||11)
         System.out.println("30");
      else if (month == 2)
         System.out.println("28");

I keep getting the error message "operator || cannot be applied to boolean,int." Can anyone help me figure out what to do?

Upvotes: 0

Views: 9133

Answers (5)

haozhe heng
haozhe heng

Reputation: 1

So if you want a basic code(not using Maps or other special functions), you could use switch for easier reading. For example :

public static void daysInMonth(int month) {
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            System.out.println("31");
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            System.out.println("30");
            break;
        case 2:
            System.out.println("28");
            break;
    }
}

The way it works is switch will continue to perform until for every case until it sees break. The following code will print out both 0 and 1.

public void test() {
    int a = 0;
    switch (a) {
        case 0:
            System.out.println("0");
        case 1: 
            System.out.println("1");
            break;
    }
}

Yes it is very long and inefficient but still is a basic solution. Not very recommended unless you are a beginner.

Upvotes: 0

kapex
kapex

Reputation: 29999

You can avoid any switch or if/else statements with the new Java 1.8 time API using java.time.Month:

public static int daysInMonth(int month) {
    return Month.of(month).minLength();
}

Upvotes: 2

Alberto Saito
Alberto Saito

Reputation: 271

You want something like this:

if ( (month == 1) || (month == 3) || (month == 5) || (month == 7)
                  || (month ==  8) || (month == 10)

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240948

|| will result into boolean and then boolean || int is not valid action

what you need is

month == 1 || month == 3 || ...

or a Map<Integer, Integer> monthNumberToMaxDaysMap

also this way you don't have leap year scenario covered, it is suitable to re-use already solved problem

 public static void daysInMonth(int month) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, month -1);
    return cal.getActualMaximum(Calendar.DAY_OF_MONTH)
 }

above method only considers current year, you could leverage it to pass year as well

Upvotes: 0

chenchuk
chenchuk

Reputation: 5742

You can use the || operator on a boolean expression :

if (month == 1 || month == 3 || month == 5) {
    // do something ...
}

Upvotes: 1

Related Questions