user4126362
user4126362

Reputation:

Java - How to perform operations on a value in an array?

This is the operation i am trying to do. I get the error "bad operand types for binary operator "*".

How do i pass this calcualtion? Can somone show me an example of how to do this? Note that day[i] has all its values already in it.

Upvotes: 0

Views: 76

Answers (1)

chiastic-security
chiastic-security

Reputation: 20520

The complaint is that day[i]*9 doesn't make any sense. This must be because day[i] isn't a numerical type.

If day is also of type Temperature[], then you can't multiply day[i] by anything because it's an object, not a number. You'll need to write a method of Temperature that allows you to read the value stored there:

public class Temperature {

    private double temp;

    // constructors etc.

    public double getTemp() {
        return temp;
    }
}

Now you can use

(day[i].getTemp()*9)/5+32

to get what you want. Note that I've set the type as double, because it looks like you'll want floating point arithmetic here.

Upvotes: 1

Related Questions