Hi everyone,

I'm trying to write a basic program to find the root of an equation. I just tried compiling it for the first time and I get the following error:

``````RootFinder.java:6: <identifier> expected
public static double searchValue(double x0, x1, a) {
^
RootFinder.java:6: <identifier> expected
public static double searchValue(double x0, x1, a) {
^
2 errors
``````

Here's my program so far:

``````public class RootFinder {
public static final double EPSILON = 0.00001;
public static double function(double x) {
return (Math.log(x + 1) + 1);
}
public static double searchValue(double x0, x1, a) {
double x;
boolean valid = (function(x0)<a) && (a<function(x1));
if (!valid) {
x = -1;
}
else if ( (x0 <= -1)||(x1 <= -1) ) {
x = -1;
}
else {
x = (x0 + x1)/2;
}
return function(x);
}
public static void main(String[] args) {
System.out.println("Enter a value for x0:");
System.out.println("Enter a value for x1:");
System.out.println("Enter a value for a:");
searchValue(x0, x1, a);
if (function(x) == NaN) {
System.out.println("No such value exists, try again…");
} else if(Math.abs(function(x) - a) > EPSILON) {
System.out.println("The root of the equation [f(x) = ln(x + 1) +1] is " + function(x));
System.out.pringln("However the difference between the root and 'a' is too great. You can do better…");
} else {
System.out.println("The root of the equation is " + function(x));
}
}
}
``````

I'm just not sure what I've done wrong...

You need to specify the type for each parameter in the method definition. You've only give a type (double) for the first one. Add type definitions for the other two parameters.

Stupid mistake... Thanks!

K, now it's saying that it can't find the symbol 'x' in the main method (starting at line 29), but I'm calling it from the searchValue method, so shouldn't that be fine?

Here's the code again with some slight changes:

``````public class RootFinder {
public static final double EPSILON = 0.00001;
public static double function(double x) {
return (Math.log(x + 1) + 1);
}
public static double searchValue(double x0, double x1, double a) {
double x = 0;
boolean valid = (function(x0)<a) && (a<function(x1));
if (!valid) {
x = -1;
}
else if ( (x0 <= -1)||(x1 <= -1) ) {
x = -1;
}
else {
x = (x0 + x1)/2;
}
return x;
}
public static void main(String[] args) {
System.out.println("Enter a value for x0:");
System.out.println("Enter a value for x1:");
System.out.println("Enter a value for a:");
searchValue(x0, x1, a);
if (x == -1) {
System.out.println("No such value exists, try again…");
} else if(Math.abs(function(x) - a) > EPSILON) {
System.out.println("The root of the equation [f(x) = ln(x + 1) +1] is " + function(x));
System.out.println("However the difference between the root and 'a' is too great. You can do better…");
} else {
System.out.println("The root of the equation is " + function(x));
}
}
}
``````

Where is the variable: x defined? Is its definition in scope (within the same pair of {}s) where you are trying to use it?

x is defined on line 7, then I return it back to the main method on line 18. So once I call the searchValue method from my main method (on line 28), shouldn't that allow my main method to see the variable x? So technically it's not in between the same {}'s, but I'm calling it from another method... Have I misunderstood something really basic here?

technically it's not in between the same {}'s,

Then it's not in scope and can't be used. The method searchValue() returns the value of x but the calling of the method on line 28 ignores what the method returns. You should assign what that method returns to a variable so it can be used in the statements after line 28.

Oh, so you mean I should write something like `double x = searchValue()` in the main method? Would I do that before or after I call the searchValue method (FYI, I will eventually be writing in a loop to keep on narrowing down the root of the equation)? And would I have to put the variables (x0, x1, and a) in the empty brackets in that line of code I just wrote?

Yes that is exactly what you should write on line 28. I don't understand what the "before or after" part means.

put the variables (x0, x1, and a) in the empty brackets

Yes.

"before or after" --> I don't exactly understand what I'm doing on line 28. That's just me 'calling a method', right? What does that mean exactly? Sorry, I'm still pretty new to all this...

and your suggestion worked, btw. Thanks! Haha now I just need to figure in the loop somehow...