Incopatable types [Resolved]
Hey I am working on a school project and I need help with 3 errors I keep getting when I compile.
I have this array storing my strings.
Code:
private String[] months = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
and I need the program to Return the Month that rainfall is the greatest.
Code:
public double getHighestMonth(){
double highest = values[0];
String month = months[0];
for (int index = 1; index < values.length; index++){
if (values[index] > highest)
highest = values[index];
month = months[index];
}
return month;
}
Here is the errors I am receiving.
Code:
RainFall.java:35: incompatible types
found : java.lang.String
required: double
return month;
^
RainFall.java:40: incompatible types
found : java.lang.String
required: double
double month = months[0];
^
RainFall.java:45: incompatible types
found : java.lang.String
required: double
month = months[index];
^
3 errors
Any help would be greatly appreciated.:check:
Re: Incopatable types [Resolved]
No, it just ensured that both the statements executed only if the IF condition were true. Without the { }, only the first statement would execute if the condition were true. The second statement, assigning the month, would execute all the time.
My guess is without the { } you were always getting the last month, in both getHighest and in getLowest functions. Another guess is your data has the maximum value in the last place.
You can check for such errors by changing the data you use, the values array in this case. Try making the third value as the highest, or the seventh value as the lowest and see if the output changes accordingly. Testing with a single set of data will sometimes make you overlook such errors.
.