Here is the error code:
Description Resource Path Location Type
result cannot be resolved to a variable DistanceBetweenTwoPoints.java /06-02-Example/src line 30 Java Problem
Here is the output from the method when the println statement in main is commented out:
dx is 3.0
dy is 4.0
dsquared is 25.0
distance is 5.0
How can I successfully return the value from the method?
Any helpful assistance will be greatly appreciated.
You have the method "distance" which returns the result. So, think about it as your result being returned to line 28, from where you're calling it.
Will Klem wrote:Returning the resulting value is where I am hitting a dead end. The compiler error indicates that the result cannot be resolved to a variable.
There are two options to solve that quickly:
1. on line 28 you do "double result = DistanceBetweenTwoPoints.distance(1.0, 2.0, 4.0, 6.0);" in this case "result" will hold the returned value
2. put line 28 inside line 30 print statement, it will return value and will print out without storing it.
I have a copy of that book, too. I found it full of errors.
Will Klem wrote:I am using a free pdf book (Think Java - How to Think Like a Computer Scientist) . . . to try to teach myself Java. Eclipse mars is my IDE, . . .
If you are a beginner you are probably better off not using an IDE.
Liutauras means to get rid of line 28 and change line 30 to read
System.out.println("Result = " + distance(x1, y1, x2, y2));
Or change the earlier line to
double result = distance(...);
The error message means you do not have a variable called result.
Thanks for your rapid replies. Both options supplied by Liutauras worked properly:
/* this solution #1 works:
* double result = DistanceBetweenTwoPoints.distance(1.0, 2.0, 4.0, 6.0);
*/System.out.println("result = " + result);
*/ this solution #2 works:
* System.out.println(DistanceBetweenTwoPoints.distance(1.0, 2.0, 4.0, 6.0));
I really appreciate having two options provided. This has taught me two more advanced concepts which may have taken me a long time to discover:
1) A method call can be assigned to a variable to obtain a return value.
2) A method call can be used as an argument s demonstrated with System.out.println().
I, too, have encountered errors in Think Java. Some I have figured out, and others I have just passed by.
Thanks to both of you for sharing your knowledge and experience with me.
 added quote
Oracle's tutorial wrote:Not all combinations of instance and class variables and methods are allowed:
Instance methods can access instance variables and instance methods directly.
Instance methods can access class variables and class methods directly.
Class methods can access class variables and class methods directly.
Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.