Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method in Class Cannot Be Applied to Given Types

 
Michelle Purdy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michelle Purdy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!!
 
Alex Petsche
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Petsche
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks for the info. I'll make sure to start a new thread if I have anymore questions.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49786
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49786
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic