Sam Barnhardt
Sam Barnhardt

Reputation: 1

I am getting an "incompatible types" error right here and I cannot figure out why

Please help to find where my code is "incompatible type"....I've looked and looked and looked and can't find it anywhere.

import java.util.Scanner;

public class Years
{
    private int centuries, decades;

    public Years(int years)
    {
        centuries = years / 100;
        years -= 25 * years;
        decades = years / 10;
        years -= 10 * years;
    }
    public int getCenturies()
    {
        return centuries;
    }
    public int getDecades()
    {
        return decades;
    }
    public static void main(String[] args)
    {
        int years;

        if(args.length >= 1)
        {
            years = Integer.parseInt(args[0]);
        }
        else
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter the amount in years: ");
            years = keyboard.nextInt();
            keyboard.close();
        }

        Years y = new Years(years);
        System.out.println(years = "y =" + 
        y.getCenturies() + "c +" + y.getDecades() + "d"); // I am getting the error right here.
        }
}

Upvotes: 0

Views: 69

Answers (1)

Radiodef
Radiodef

Reputation: 37835

System.out.println(
    years = "y =" + y.getCenturies() + "c +" + y.getDecades() + "d"
);
//  ^^^^^^^

The problem is years =. The compiler is not really sure what to do with this. The result of the right hand side of the = is a String because you are performing String concatenation.

So the compiler thinks you are doing this:

years = ("y =" + y.getCenturies() + "c +" + y.getDecades() + "d")

years is an int so this is not a valid expression.

You probably just meant this:

System.out.println(
    "y = " + y.getCenturies() + "c + " + y.getDecades() + "d"
);

Or possibly to concatenate years somewhere in there:

System.out.println(
    years + "y = " +
    y.getCenturies() + "c + " + y.getDecades() + "d"
);

Upvotes: 3

Related Questions