Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Referencing to Method. Incompatible Types Error.

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Referencing to Method. Incompatible Types Error.

    I'm gonna come out straight, this is a homework assignment. I almost got it done, but there is one main issue keeping me from compiling and finishing this. I have two classes. One is a tester (main method) and the other class containing the constructor with the other methods. In a for loop in the tester class, I have a line of code that is written out as:

                distance[index] = fillup[index].calcDistance();

    I get the error on this line and and the IDE is pointing to the parentheses at the end of calcDistance saying Incompatible Types.

    In the other class with the constructor I have a method written as:
      public void calcDistance  (){
            myDist = myEndMiles - myStartMiles;
        }
    and
     public int getDistance (){
            return myDist;
        }
    The error explains that it detects a different type, where it expects something but some other type is placed there instead. Well I maintained the int type and there are no arguments so I don't really see what the problem is. I even have it written out similar to the example program. I have all the variables declared and everything. If you need more code to see let me know.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    The incompatible types message occurs because you call calcDistance() which is a void method. That is, it does not return anything. But you try and assign the result to distance[index]. You can't assign the result to anything if nothing is being returned.

    getDistance() does return something, so perhaps you should think about how it could be used.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    Hmm, well I edited
    distance[index] = fillup[index].calcDistance();
    to say
    distance[index] = fillup[index].getDistance();
    .

    It compiled but my output numbers in the terminal are all F'd up. Worse than before. I got all 0's. I suppose that wasn't the solution. The thing is though in the example program there is something written as:

    public void calcTriArea()
        {
            myArea = mySide1 * mySide2 * .5;
        }
     
        public double getTriArea()
        {
            return myArea;
        }

    So I have a feeling the way I had it was right, but maybe something else was wrong.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    It compiled but my output numbers in the terminal are all F'd up.
    That's why I suggested thinking about how getDistance() might be used

    getDistance() will return something, so that's good. But you still have to calculate the distance. Have a look at the example program: getTriArea() will have been used to get the area, but calcTriArea() will also have been called somewhere in order to calculate the area that getTriArea() will later return. You have to do something similar.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    I thought I did that with myDist = myEndMiles - myStartMiles; in the calcDist method. Sorry I can't think straight. I have been on this literally all day .

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    Yes calcDist() will correctly calculate the distance. But you have to call this method.

    You have to call calcDist() to calculate the distance. And then you can use getDistance() to get the correct distance.

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    I'm just guessing about the triangle example, but I imagine it would be along the lines of:

     
     
    public class RightTriangle 
    {
        private double myArea;
        private double mySide1;
        private double mySide2;
     
        public RightTriangle(double s1, double s2)
        {
            mySide1 = s1;
            mySide2 = s2;
        }
     
        public void calcTriArea()
        {
            myArea = mySide1 * mySide2 * .5;
        }
     
        public double getTriArea()
        {
            return myArea;
        }
     
        public static void main(String[] args) 
        {
            RightTriangle test = new RightTriangle(3, 4);
     
            test.calcTriArea();                    // first calculate
            System.out.println(test.getTriArea()); // then get
        }
    }

  8. #8
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    Dang, you guessed pretty good. Though that example is in 2 classes as well. I tried plainly making a separate statement in the main saying something like fillup.calcDistance(); but it didn't recognize the method (cant find symbol). I am like beyond baffled right now. Should I post the entirety of both scripts?

  9. #9
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    Shouldn't you be saying something like fillup[index].calcDistance();

    Yes, I know! you did say that. But do both. First fillup[index].calcDistance(); then whatever=fillup[index].getDistance();

  10. #10
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    fillup[index].calcDistance(); didn't come up as an error and seemed to be a potential solution. But the thing is I have to go by certain guidelines. For example:

    for (int index = 0; index < fillup.length; index++)
    { 
     
                distance[index] = fillup[index].calcDistance();
                mpg[index] = fillup[index].calcMPG(distance[index]); 
                cost[index] = fillup[index].calcCost(); 
                ...
     
    }

    The above is actually code given to me. Meaning I'm not really supposed to change it. Then the time I'm supposed to use the Getter method is when I'm printing it. It's gotta look something like:

    for (int x = 0; x <= 3; x++){
                System.out.printf("%4d %9d %12d %12d %12d %12.2f %11.1f %8.2f %8.2f\n", x+1, fillup[x].getDays(), fillup[x].getStartMiles(), fillup[x].getEndMiles(), 
                fillup[x].distance(), fillup[x].getGallonsUsed(), fillup[x].getGallonsUsed(), mpg[x], fillup[x].getPricePerGallon(), fillup[x].getCost());
            }

    Of course there might be a few things that needs to be fixed in the output statement. Perhaps I should try those suggested statements in a separate loop?

  11. #11
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    That puts you back to square one.

    You say you must use "distance[index] = fillup[index].calcDistance();", but calcDistance() is a void method (does not return anything). Under those conditions the compiler message is inevitable.

    The whole point of the compiler message is to indicate that *something* must changed. Perhaps you are supposed to write calcDistance() so that it returns a value.

  12. #12
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    Well, one thing I noticed is that I'm only supposed to have 5 private instance variables. I added 3 more to use for the mutator methods (which I need). I think somehow I'm supposed to build the calcDistance without a private variable and still use the accessor methods (with private variable). I'm not really sure how to do this though.

    EDIT: FIGURED.IT.OUT.

    After noticing a strange contradiction between the instructions and the example. It all played out. The mutator method in the example didn't return void, but a double. Then one of the in-line comments suggested that those methods should return a void (but I ignored that). I changed my mutators to look like:

    public int calcDistance(){
            return myEndMiles - myStartMiles;
        }

    and to return the calculation result itself and not a variable. Then getting to the accessor methods, I just returned the calcDistance method like so:

    public int getDistance(){
            return calcDistance();
        }

    Although I am still getting the strange output numbers I did before, at least they aren't zeros and I can actually compile. Just in case this isn't the absolute solution, I won't mark this as solved yet.
    Last edited by mwebb; February 5th, 2012 at 02:37 PM.

  13. #13
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referencing to Method. Incompatible Types Error.

    EDIT: Fixed output too. The most obvious mistake I somehow managed to surpass took me almost 24 hours to notice...

Similar Threads

  1. [SOLVED] Bad operand types for binary operator '<' error issue (beginner)
    By mju516 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 27th, 2012, 10:33 PM
  2. incompatible types!!
    By sneha343 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2011, 05:48 PM
  3. incompatible types
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2011, 10:40 AM
  4. incompatible types Exception
    By world.java in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 17th, 2010, 01:33 PM
  5. Incompatible Types
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2010, 09:52 AM