Hi, I am having some troubles with a recursive function.
The function needs to get a string as an input. The string contains both symbols and numbers. What the function does is doing some operation on the two children of a node, the node being the operation(+,-,*,/).
The thing is, I need the function to return a double. This couses a problem since I cant just convert the string to a double becouse of the operators.
This is the algoritm:

function Evaluate {
if node is an operator(+,-,*,/) then
    evaluate the right and left child
    apply the operator of the node to the two children
else if the node is a value then
    the result is the value

Recommended Answers

All 4 Replies

You shouldn't ever be returning the operators though, only the result of operations or the number value in the node.

In the first branch, after you apply the operator to the two children, you should get back a double then return that.

In the second branch you should be returning the value (which is a double).

commented: thank you +1

You are right. It will never return anything but a double.
I wrote some code. The problem now is that I get a NullPointerException which I dont get. Every operator has two children so the none of the children should be null.

public Double eval()
    {
         Double answer = null;
         if(data != null)
         {
            if(data.equals("-")) answer += (rightChild.eval() - leftChild.eval());
            else if(data.equals("+")) answer += (rightChild.eval() + leftChild.eval());
            else if(data.equals("*")) answer += (rightChild.eval() * leftChild.eval());
            else if(data.equals("/")) answer += (rightChild.eval() / leftChild.eval());
            else answer += ((Double)data).doubleValue();
        }
        
       
        return answer;
    }

[EDIT]

I changed the code to this:

public Double eval()
    {
         Double answer = 0.0;
        
            if(data.equals("-")) answer += (rightChild.eval() - leftChild.eval());
            else if(data.equals("+")) answer += (rightChild.eval() + leftChild.eval());
            else if(data.equals("*")) answer += (rightChild.eval() * leftChild.eval());
            else if(data.equals("/")) answer += (rightChild.eval() / leftChild.eval());
            else answer += ((Double)data).doubleValue();
            
        return answer;
    }

now I get "java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double"

I think this is all you need to do, although to be honest I'm not the best at recursion myself, but if everything else is right this will fix your error I believe. Double.parseDouble(data);

commented: thanks for the help +1

data was an Object, I changed it so a String and did Double.parseDouble(data); instead of ((Double)data).doubleValue();
Now it works. Thank you both :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.