user4339745
user4339745

Reputation:

After I compile my Java program, it says error: incompatible types: double cannot be converted to double[]

What can I do to fix this?

Here's part of the beginning of the code:

double fee;
double tuition;
double[] residence, total;

Here's the part where it's wrong:

total = tuition + fee;

error: incompatible types: double cannot be converted to double[]

What do I do to fix this?

Upvotes: 0

Views: 3788

Answers (3)

Squirrel
Squirrel

Reputation: 2282

What you've done here is declared 4 variables: fee and tuition are of type double, while residence and total are of type double[] -- i.e. an array of elements of type double.

You're adding up tuition and fee, and what the compiler is expecting you to do is to put the result into another variable of type double, but your code is asking to store it into total which is of type double[] (array of double), and the compiler doesn't know how to resolve that.

You can either

  1. Tell the compiler which element of total to store the result in, for example:

    total[0] = tuition + fee
    
  2. Declare total as having the type of a single double instead of an array:

    double fee;
    double tuition;
    double total;
    double[] residence;
    
    // this is now okay
    total = tuition + fee;
    
    // this is again a type error because residence is still an array
    residence = total;
    

Upvotes: 1

Predrag Maric
Predrag Maric

Reputation: 24433

I think you meant to do this, correct me if I'm wrong

double tuition, residence, total;

The way you did it results in residence and total being arrays of double values, and not a double value like tuition.

Upvotes: 0

brso05
brso05

Reputation: 13232

For an array you must specify an index. Like:

total[0] = tuition + fee;

An array is a collection of something in this case doubles an array cannot equal one double it can have several double values at different indexes of the array.

Upvotes: 0

Related Questions