943,769 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 757
  • Java RSS
Nov 30th, 2008
0

recursive function problem

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efus is offline Offline
15 posts
since Oct 2008
Nov 30th, 2008
1

Re: recursive function problem

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).
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Nov 30th, 2008
0

Re: recursive function problem

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.

Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efus is offline Offline
15 posts
since Oct 2008
Nov 30th, 2008
1

Re: recursive function problem

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);
Reputation Points: 11
Solved Threads: 1
Newbie Poster
shaun.husain is offline Offline
4 posts
since Nov 2008
Nov 30th, 2008
0

Re: recursive function problem

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efus is offline Offline
15 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: need help with part of program
Next Thread in Java Forum Timeline: Please help! I need to get subscript.....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC