recursive function problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 11
Reputation: efus is an unknown quantity at this point 
Solved Threads: 0
efus's Avatar
efus efus is offline Offline
Newbie Poster

recursive function problem

 
0
  #1
Nov 30th, 2008
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:
  1. function Evaluate {
  2. if node is an operator(+,-,*,/) then
  3. evaluate the right and left child
  4. apply the operator of the node to the two children
  5. else if the node is a value then
  6. the result is the value
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: recursive function problem

 
1
  #2
Nov 30th, 2008
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).
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: efus is an unknown quantity at this point 
Solved Threads: 0
efus's Avatar
efus efus is offline Offline
Newbie Poster

Re: recursive function problem

 
0
  #3
Nov 30th, 2008
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.

  1. public Double eval()
  2. {
  3. Double answer = null;
  4. if(data != null)
  5. {
  6. if(data.equals("-")) answer += (rightChild.eval() - leftChild.eval());
  7. else if(data.equals("+")) answer += (rightChild.eval() + leftChild.eval());
  8. else if(data.equals("*")) answer += (rightChild.eval() * leftChild.eval());
  9. else if(data.equals("/")) answer += (rightChild.eval() / leftChild.eval());
  10. else answer += ((Double)data).doubleValue();
  11. }
  12.  
  13.  
  14. return answer;
  15. }



[EDIT]

I changed the code to this:
  1. public Double eval()
  2. {
  3. Double answer = 0.0;
  4.  
  5. if(data.equals("-")) answer += (rightChild.eval() - leftChild.eval());
  6. else if(data.equals("+")) answer += (rightChild.eval() + leftChild.eval());
  7. else if(data.equals("*")) answer += (rightChild.eval() * leftChild.eval());
  8. else if(data.equals("/")) answer += (rightChild.eval() / leftChild.eval());
  9. else answer += ((Double)data).doubleValue();
  10.  
  11. return answer;
  12. }

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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: shaun.husain is an unknown quantity at this point 
Solved Threads: 1
shaun.husain shaun.husain is offline Offline
Newbie Poster

Re: recursive function problem

 
1
  #4
Nov 30th, 2008
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);
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: efus is an unknown quantity at this point 
Solved Threads: 0
efus's Avatar
efus efus is offline Offline
Newbie Poster

Re: recursive function problem

 
0
  #5
Nov 30th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 510 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC