| | |
recursive function problem
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
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:
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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).
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).
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.
[EDIT]
I changed the code to this:
now I get "java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.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.
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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"
Last edited by efus; Nov 30th, 2008 at 5:37 pm.
![]() |
Similar Threads
- reversing number problem. (C)
- how can i solve Queen problem please help me (C++)
- recursion problem... (Java)
- Recursive function - checking for palindromes (C)
- need help reversing letters in a word (Java)
- Recursive help needed badly! (Java)
- recursive findaverage function for Binary search tree (C)
- Recursive prime number f(x) (C)
Other Threads in the Java Forum
- Previous Thread: need help with part of program
- Next Thread: Please help! I need to get subscript.....
Views: 510 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android api apple applet application arguments array arrays automation binary block bluetooth chat class classes client code compile component database developmenthelp draw eclipse encode error event exception file fractal freeze game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number object online oracle print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing system template test textfields threads time title transfer tree tutorial-sample update windows working





