0

What's wrong with my code? The error is "unexpected type." It's the if (year % 4.0 = 2.0) part and the error message is

assignment4part2.java:18: error: unexpected type
               if (year / 4.0 = 0.0)
                    ^
  required: variable
  found:    value

The code is as follow:

import java.util.Scanner;

public class assignment4part2 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number of a month");
        double month = input.nextDouble();
        System.out.println("Enter a year");
        double year = input.nextDouble();
        if (month == 1.0) {
           System.out.println("January " + year + " had 31 days.");
        } else if (month == 2.0) {
           if (year % 4.0 = 0.0) {
               System.out.println("February " + year + "had 29 days.");
           } else {
               System.out.println("February " + year + "had 28 days.");
           }
        } else if (month == 3.0) {
            System.out.println("March");
        }
    }
}
yanana
  • 2,241
  • 2
  • 18
  • 28
John Lee
  • 49
  • 1
  • 3
  • Also, as an FYI. A leap year happens under the following condition `if (((month % 4 == 0) && (month % 100 != 0)) || (month % 400 == 0)){` – Timothy Murphy Jul 20 '15 at 18:33

1 Answers1

3

As @Maxime said, you need == instead of =. This is because the compiler expects a boolean value inside of an if statement, and the = operator returns the value that is on its right. (In this case, a double).

A few more comments related to your code, but not the question:

  1. Be careful when using == with double types. Generally it is considered to be much better practice to use an EPSILON instead of checking for equality when working with floating point numbers:

    if (year % 4.0 == 0.0)

    Should be:

    if (Math.abs(year % 4.0 - 0.0) < EPSILON ), where EPSILON is a very small double value that specifies how close year % 4.0 has to be to 0.0. In this case it is clearly redundant to subtract 0.0, but using it in the example helps illustrate the template.

  2. Using the % operator with floating point numbers may give you unexpected results due to rounding errors and the way floating point numbers are stored. If you need the % operator, consider using some type of integer instead. From your posted code, it seems entirely appropriate for you to be using int or long instead of double. (You can read more about doubles and % here).

  3. It seems as though you are using multiple if else statements one after another. There is a construct intended specifically for the type of control flow you are aiming to achieve called a switch statement. (You can read more about switch statements here). Again, be careful using switch statements with floating point variables.

  4. Finally, in the future it will help people provide better answers if you tag your question with the language you are using. (In this case, Java).

Community
  • 1
  • 1
Matt Martin
  • 807
  • 2
  • 8
  • 15