aspose file tools*
The moose likes Beginning Java and the fly likes Method in Class Cannot Be Applied to Given Types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method in Class Cannot Be Applied to Given Types" Watch "Method in Class Cannot Be Applied to Given Types" New topic
Author

Method in Class Cannot Be Applied to Given Types

Michelle Purdy
Greenhorn

Joined: Oct 30, 2012
Posts: 10
We use BlueJ in our Java course and for this one assignment, I have two classes: Car and GasTank. The method drive() generates a random number of miles for one segment of a trip. Then I needed to write code to check to see if the gas tank is empty. If it is, do not allow movement of the car. If it isn't, call the burnGas() method of in the GasTank class. Then the printTripDetails() method is called to show the current status of the trip. Well, when I set up the If statement to check if the gas tank was empty, I get the following error message, which is Greek to me and I'm so confused. I have tried so many different things and nothing is working!! I'm really concerned that this is super simple stuff that I can't figure out and if I'm not understanding this then maybe I'm in the wrong field??

method burnGas in class GasTank cannot be applied to given types;
required: double, double
found: no arguments
reason: actual and formal argument lists differ in length

The operator that you use here cannot be used for the
type of value that you are using it for. You are either
using the wrong type here, or the wrong operator.


Here is the code for Car, which probably has more errors than just what I've mentioned here:


and this is the code for GasTank, which again, has its own errors that won't compile:
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11418
    
  16

the compiler is telling you the problem. You have defined the burnGas method to take two parameters:


You have to tell it how far you are driving, and what the MPG is so it can compute how much gas is needed. But on line 81, you call burnGas with no parameters:


Java is telling you that there is no such method called burnGas that takes no parameters.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Michelle Purdy
Greenhorn

Joined: Oct 30, 2012
Posts: 10
fred rosenberger wrote:the compiler is telling you the problem. You have defined the burnGas method to take two parameters:


You have to tell it how far you are driving, and what the MPG is so it can compute how much gas is needed. But on line 81, you call burnGas with no parameters:


Java is telling you that there is no such method called burnGas that takes no parameters.


Yeah, see, I don't understand the compiler's language. But, I tried putting in tank.burnGas(milesDriven,mpg); and it takes me to a compile error in the GasTank class in the percentTankFull()
method. I don't understand that. What's that have to do with tank.burnGas()?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11418
    
  16

there may not be any connection. But remember, the compiler is dumb. When it finds errors, it tries to figure out where the problem might be. Sometimes, the error is so bad that it gives up and says "Here is the one error I found". Then, when you fix it and compile again, it gets past that spot, but then finds more errors. The errors were there all along, but it just bailed out when it found the first one, not bothering to find any more.

It is impossible for us to help you if you don't post your current code, and what the current error is. But if you take your time and read it, they really do give you a pretty good idea what the problem is. If you can't figure it out, post your code here and the complete error (or at least the first few lines if it is really long). someone will be glad to help.
Michelle Purdy
Greenhorn

Joined: Oct 30, 2012
Posts: 10
Well, I got both classes to compile, but I don't think the program is working correctly. I had to add a getSizeOfTank() method and add a return statement to the fill() method all in the GasTank class, but I think I did it all wrong. I find Java very confusing!!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11418
    
  16

I might also add that you are probably doing this all wrong. You have about 175 lines of code, and you are just finding compiler errors now.

I never write more than 2-3 lines of code before I re-compile and fix any problem i find. I test as I go, writing a lot of code I later throw away to help me test. I would strongly suggest you delete most of what you've written, and write smaller chunks at a time.
Michelle Purdy
Greenhorn

Joined: Oct 30, 2012
Posts: 10
Well, here's with my modifications. We're supposed to run the Drive() method and if it runs out of gas, it shouldn't move anymore and then we Fill() it up. But I just keep getting the default info listed in the constructor.

Car:


GasTank:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18659
    
    8

I'm not quite clear on what your problem is, since you didn't post any code which uses those classes. But I just thought I would point out one thing:



If you're going to put comments in your code (and yes, you should do that), then the code should reflect what the comments say. This code doesn't do that.
Michelle Purdy
Greenhorn

Joined: Oct 30, 2012
Posts: 10
Yeah, unfortunately, I didn't write most of it. I think our instructor did. We are to go in and modify/add, etc. and it's so confusing! I think I'm just going to have to wait until class tomorrow. The program is due tonight, and I haven't been able to make any sense of it or another assignment from him that I've been working on for the past week and he's too hard to get a hold of/never responds to me and I live an hour away from campus. Kinda makes learning difficult! But I certainly appreciate your help!!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18659
    
    8

Well, good luck. It does sound as if you need some face-to-face help to get you on track... hopefully that will get you pointed in the right direction.
Michelle Purdy
Greenhorn

Joined: Oct 30, 2012
Posts: 10
Thank you!!
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

This may be a little off topic, but don't you need a "main" method in a class for the program to run? I don't see one in the code posted. Just a thought, but if I'm right could it be that it's not running correctly because there isn't a "main" method? If there is a "main" method could you post it so that we can see how you are calling each method in the classes? (or at least where your starting point is)
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Alex Petsche wrote:Just a thought, but if I'm right could it be that it's not running correctly because there isn't a "main" method?

No. The error message OP posted is compile time error, so the code couldn't even compile. What you're talking about would be runtime error with totally different content.


The quieter you are, the more you are able to hear.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

Ah ok. Thanks for clearing that up. Just to make sure I've got it straight a compile error is when the code logic doesn't follow through and a run error means that the code logic is correct, but the output isn't what you wanted? I guess I'm not sure what the difference is otherwise.

(Not wanting to hijack this thread). I think you need to put values to send to burnGas();. In GasTank it askes for two doubles, but when you call it in the Car class you aren't sending anything to it.



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alex Petsche wrote:Ah ok. Thanks for clearing that up. Just to make sure I've got it straight a compile error is when the code logic doesn't follow through and a run error means that the code logic is correct, but the output isn't what you wanted? I guess I'm not sure what the difference is otherwise.


I compile-time error happens during the process of compiling your .java file into a .class file. It occurs when there is something in your .java file that is not legal in the Java language, such as missing a semicolon or misspelling a class or method name, or trying to assign a value to a type that's not compatible to that value.

A runtime error is one that occurs when executing your code.

Make sure you're clear on the difference between the compilation step and the execution step.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Compile time error would be the one detected by the compiler. These might be caused for instance by violation of Java syntax rules (e.g. you forgot to close quote when declaring a string variable), or its semantics (incompatibility between the signature of method you declared and an actual method call, which was the OP genuine problem). These errors result in failing to compile a source.
Runtime errors on the other hand are detected by Java interpreter, when trying to execute a program that is already compiled (hence, compiler didn't detect any of those errors above). These usually result in throwing an exception (e.g. NullPointerException) or showing an error message (e.g. NoSuchMethodFoundError which is what you mentioned earlier about main method).

And yes, this is probably a question for a new topic, so if you would like to hear someone else's opinion you should probably start new one.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

Ok thanks for the info. I'll make sure to start a new thread if I have anymore questions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Welcome to the Ranch

Nobody noticed that you told us one serious problem:-
Michelle Purdy wrote:We use BlueJ . . .
Yes, the first three words you posted told us that. At least those of us who have suffered from BlueJ ourselves.
I do not like it. It permits you to write half an application and run it. Another annoying habit it had was never finding compiler errors. It would find a compiler error, just one, and throw an error message. If you can sort out the error there and then, all well and good, because sometimes one error causes multiple error messages. But if you can’t, or even worse, if the second error message would have been more helpful, you never see the second error.
And when I got the error sorted out, the code still wouldn’t compile because there were multiple errors and it had only told me about the first one.

Copy your entire text into (2) .java files with a decent text editor (ie not MS NotePad), and compile it from the command line with
javac Car.java GasTank.java
…and get a full list of compiler errors.

By the way, for those who use == and != in boolean expressions, look here to see why that can be a Bad Thing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
fred rosenberger wrote:. . . But remember, the compiler is dumb. . . .
That is unfair. Maligning the compiler like that, Fred!

It would be more accurate to say that writing compilers for correct code is reasonably straightforward, but writing compilers for incorrect code is much more difficult. You cannot anticipate what sort of mistakes people will make, so you have to guess at the most likely errors. It is very hard to provide helpful error messages. I must say, the compiler which comes with Eclipse does a far better job with error messages than the Sun/Oracle version.
You may find this link helpful about error messages.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method in Class Cannot Be Applied to Given Types