Using If-Else-If statement but it keeps saying it is an invalid entry

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bryantbe
    New Member
    • Oct 2013
    • 2

    Using If-Else-If statement but it keeps saying it is an invalid entry

    Code:
    System.out.println("Enter the day of the month. ");
      dayOfMonth = keyboard.nextInt();
       
       System.out.println("Enter the month. ");
      monthName = keyboard.nextLine();
       
       System.out.println("Enter the year. ");
      int Year = keyboard.nextInt();
     
      if (Year/4){
      if (Year/ 4 && Year/100)
       {isItLeapYear=true;}
      else if (Year/4 && Year/100 && Year/400)
       {isItLeapYear=true;}
     } else {isItLeapYear=false;}


    it says that I cannot use Year/ because it is an int not Boolean..
    Last edited by Rabbit; Oct 23 '13, 05:31 AM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • bryantbe
    New Member
    • Oct 2013
    • 2

    #2
    This is what is says NumDaysLeftInYe ar.java:61: error: incompatible types
    if (Year/4){
    ^
    required: boolean
    found: int
    NumDaysLeftInYe ar.java:62: error: bad operand types for binary operator '&&'
    if (Year/ 4 && Year/100)
    ^
    first type: int
    second type: int

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Please use code tags when posting code or formatted data.

      The binary operator && compares two booleans. A boolean is either true or false. Year / 4 and Year / 100 is not a true or false statement. A comparison of the result to an expected value is a true or false statement but just the result itself is neither true or false. It's like asking if 1 + 1 is true or false. It is not a true or false question. But asking if 1 + 1 is equal to 2 is a true or false question.

      Also, you should know that you don't need to check Year / 4 twice. There's no point in doing so.

      Comment

      • rajujrk
        New Member
        • Aug 2008
        • 107

        #4
        Hi,

        Please go over Java Basics.

        if statement allows only boolean type

        Code:
        if(<boolean_value>){
                -----
        }{
                -----
        }
        Year is int, then Year/4 will return int or double type

        these types are not allowed inside the if statement

        Thanks
        Raju

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          What you probably wanted is something like this:
          [code=java]if (Year % 4 == 0){
          if (Year % 4 == 0 && Year % 100 == 0)
          {isItLeapYear=t rue;}
          else if (Year % 4 == 0 && Year % 100 == 0 && Year % 400 == 0)
          {isItLeapYear=t rue;}
          } else {isItLeapYear=f alse;}[/code]
          The Modulo-operator (%) will calculate what number is left when dividing the first number by the second one; so for example 9 % 4 == 1 as there is a number x for which x * 4 + 1 == 9.

          Mind you, the else if-case will never be reached as all years that fulfil the condition is divisible by 4, 100 and 400 also fulfil the condition is divisible by 4 and 100 (which is the condition in the if-condition before the else if).

          Comment

          Working...