Results 1 to 8 of 8

Thread: Incopatable types [Resolved]

  1. #1

    Thread Starter
    Lively Member OT²O's Avatar
    Join Date
    Jan 2010
    Location
    Washington State
    Posts
    96

    Resolved 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;
    }

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3

    Thread Starter
    Lively Member OT²O's Avatar
    Join Date
    Jan 2010
    Location
    Washington State
    Posts
    96

    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;
    }

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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?

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5

    Thread Starter
    Lively Member OT²O's Avatar
    Join Date
    Jan 2010
    Location
    Washington State
    Posts
    96

    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;
    }

  6. #6
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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;
    }
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  7. #7

    Thread Starter
    Lively Member OT²O's Avatar
    Join Date
    Jan 2010
    Location
    Washington State
    Posts
    96

    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;
    }

  8. #8
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width