|
-
Jan 17th, 2011, 10:58 PM
#1
Thread Starter
Lively Member
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.
Last edited by OT²O; Jan 18th, 2011 at 03:42 AM.
Reason: resolved
On the off chance that I helped you ( Rate This Post){
Also If your problem was resolved;
}
-
Jan 18th, 2011, 01:04 AM
#2
Re: Incopatable types
Change your method return type. Instead of
Code:
public double getHighestMonth(){
make it
Code:
public String getHighestMonth(){
I think instead of posting fragments if you post the complete class, you may get better help. Anyways, double check the data types you have used in the code.
.
-
Jan 18th, 2011, 01:38 AM
#3
Thread Starter
Lively Member
Re: Incopatable types
That did not work. =[
Here is the entire program i just didn't want my classmates to have access to my code. Chances are they wont be on the forum so it's no big deal.
I will be executing it in the interactions tab of jgrasp with this.
Code:
double [] d = {1.1, 2.2, 3.3 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10, 11.11, 12.12}
RainFall rain = new RainFall(d)
and when I request Highest or lowest month i want it to give me the String of the month.
Last edited by OT²O; Jan 18th, 2011 at 03:40 AM.
Reason: Removed Code so fellow students could not cheat.
On the off chance that I helped you ( Rate This Post){
Also If your problem was resolved;
}
-
Jan 18th, 2011, 02:46 AM
#4
Re: Incopatable types
Both the functions getHighestMonth and getLowestMonth are actually returning the variable month, which is of type String.
I am assuming that the errors you listed in the first post are what you get with the above code. What errors are you getting after you change the return type of getHighestMonth and getLowestMonth to String?
.
-
Jan 18th, 2011, 03:05 AM
#5
Thread Starter
Lively Member
Re: Incopatable types
Ok, I got it working you were right!
Here is my new code but i'm having one more problem.
My lowest is also returning as December instead of January.
Last edited by OT²O; Jan 18th, 2011 at 03:39 AM.
Reason: Removed Code so fellow students could not cheat.
On the off chance that I helped you ( Rate This Post){
Also If your problem was resolved;
}
-
Jan 18th, 2011, 03:29 AM
#6
Re: Incopatable types
I think in both the functions, you have not included the if construct in { }. Try the below code and see if it works:
Code:
public String 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;
}
public String getLowestMonth(){
double lowest = values[0];
String month = months[0];
for (int index = 1; index < values.length; index++){
if (values[index] < lowest) {
lowest = values[index];
month = months[index];
}
}
return month;
}
-
Jan 18th, 2011, 03:38 AM
#7
Thread Starter
Lively Member
Re: Incopatable types
Adding the {} worked perfectly. Thank you very much for your help.
Did that just make the variables inside the code Private to that function so they were not the same in the next use?
On the off chance that I helped you ( Rate This Post){
Also If your problem was resolved;
}
-
Jan 18th, 2011, 04:04 AM
#8
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.
.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|