Computer Science Canada java.lang.StackOverflowError? |
Author: | a22asin [ Fri Oct 31, 2014 9:05 pm ] | ||
Post subject: | java.lang.StackOverflowError? | ||
Hi so im working on converting a program's function to use recursive instead of a while loop. Ive managed to quite a bit but just when i think i got it, i get a java.lang.StackOverflowError error and i cant figure out why im getting it. If i have done this right, it should separate the numbers and operations recursively and pass it to the next function to be evaluated. Unfortunately, i didnt to it right. Can someone please help me out as im a little new to recursions? Oh and the function is initially passed (string, 0,0,0).
|
Author: | Tony [ Fri Oct 31, 2014 9:36 pm ] | ||
Post subject: | RE:java.lang.StackOverflowError? | ||
How deep does your recursion goes? Far enough to run out of stack space, suggesting that it's probably stuck in an infinite loop. Pop-quiz. What's the result of
|
Author: | a22asin [ Fri Oct 31, 2014 9:41 pm ] | ||
Post subject: | Re: RE:java.lang.StackOverflowError? | ||
Tony @ Fri Oct 31, 2014 9:36 pm wrote: How deep does your recursion goes? Far enough to run out of stack space, suggesting that it's probably stuck in an infinite loop.
Pop-quiz. What's the result of
it would return 6? what do u mean by how deep? Also, i cant seem to figure out why its going into an infinite loop. The charIndex would eventually == length of the string. |
Author: | Tony [ Fri Oct 31, 2014 9:51 pm ] |
Post subject: | RE:java.lang.StackOverflowError? |
you've clearly didn't run the above code. It would return 5. edit: understanding why it's a 5 instead of 6 should explain why it's not true that Quote: The charIndex would eventually == length of the string. A good way to keep track of what's going on is to print the value of charIndex every time you enter the function -- that shows you both the progress being made (if any) and how often the function is called. |
Author: | a22asin [ Fri Oct 31, 2014 9:54 pm ] |
Post subject: | RE:java.lang.StackOverflowError? |
no i didnt run it, seemed like a simple answer, i =2, i++ so i = 3 +i =6. but i was wrong. uhm.. wat does this have to do with the recursion though? |
Author: | a22asin [ Fri Oct 31, 2014 9:56 pm ] |
Post subject: | Re: RE:java.lang.StackOverflowError? |
a22asin @ Fri Oct 31, 2014 9:54 pm wrote: no i didnt run it, seemed like a simple answer,i forgot that +i is still 2 since it wasnt declared. but i was wrong. uhm.. wat does this have to do with the recursion though? |
Author: | Tony [ Fri Oct 31, 2014 10:02 pm ] | ||||||
Post subject: | RE:java.lang.StackOverflowError? | ||||||
i++ is a post-increment and the way it works is that the return value is the current value of i, and contents of i are incremented after. so in
it progresses as
and so
there's ++i which would do what you would expect. Or just the simple i + 1 since you don't really need to assign anything back to the variable. |
Author: | a22asin [ Fri Oct 31, 2014 10:12 pm ] | ||||||||
Post subject: | Re: RE:java.lang.StackOverflowError? | ||||||||
Tony @ Fri Oct 31, 2014 10:02 pm wrote: i++ is a post-increment and the way it works is that the return value is the current value of i, and contents of i are incremented after.
so in
it progresses as
and so
there's ++i which would do what you would expect. Or just the simple i + 1 since you don't really need to assign anything back to the variable. oh ok i get it. So i changed the index++ to ++index for all of them and it works, but now im getting Error.java.lang.NumberFormatException with no source, not sure where im getting it. I have provided the code below for the whole. i tried to put some println to see where it goes wrong, but as soon as i enter the string, it gives me the error
|
Author: | Tony [ Fri Oct 31, 2014 11:00 pm ] |
Post subject: | RE:java.lang.StackOverflowError? |
if you don't catch the exception, you get the full stack trace |
Author: | a22asin [ Fri Oct 31, 2014 11:06 pm ] | ||
Post subject: | Re: java.lang.StackOverflowError? | ||
ok i ran the program without the try/catch and this is what i got:
the first line at 94 refers to the line : numbers[numIndex]=(new Double(next)); but what i dont get is if the number im entering is an integer for example ( ex. (3+4) ), then why am i still getting this error. |
Author: | a22asin [ Fri Oct 31, 2014 11:51 pm ] | ||
Post subject: | Re: java.lang.StackOverflowError? | ||
Ok so ive gone through and fixed majority of the errors. But now my answer is always coming out as 0.0. Im noticing that its not passing the case ')' or any of the math operations so it never passes the arrays to the stack evaluate. And again, I cant figure out why as the charIndex reaches the index of the ')'.
|
Author: | C14 [ Sat Nov 01, 2014 8:24 am ] |
Post subject: | RE:java.lang.StackOverflowError? |
Have a look at what is being stored in your arrays. For the code posted, and a simple test case of (2+2), you numbers are not at positions 1 and 0. Also your operator never gets stored. You would be better off using and a stack where you can push and pop values like in your comments. Then you no longer have to worry about array positions Stack numbers = new Stack(); Stack operators = new Stack(); |
Author: | a22asin [ Sat Nov 01, 2014 10:33 am ] |
Post subject: | RE:java.lang.StackOverflowError? |
Quick question, the 1st char in a string is at what index? 0 right? Also, the original program uses stacks, but my assignment is to convert it to recursion instead of stacks; and this is the way i managed to get it to work. if you have a better way of making it a recursion, im open to suggestions as this is becoming quite a pain. |
Author: | a22asin [ Sat Nov 01, 2014 10:49 am ] | ||||
Post subject: | Re: java.lang.StackOverflowError? | ||||
Ok, so i added some System.out to keep track at what char its at and its index and this is what i got with an input of (3+2):
So ya its skipping the + and the ) and i cant seem to figure out why, when i look at the code, everything seems fine to me. ![]()
|
Author: | C14 [ Sat Nov 01, 2014 11:48 am ] |
Post subject: | RE:java.lang.StackOverflowError? |
evaluate(s, ++numIndex, operatIndex+1, charIndex+1); Your numIndex should only increment after each number, the operator index should increment after each operator. The way you have it, they increment after each character regardless Stacks can be used with recursion |
Author: | a22asin [ Sat Nov 01, 2014 11:51 am ] | ||
Post subject: | RE:java.lang.StackOverflowError? | ||
oh i see, and im not allowed to use stacks, i have to replace stacks with recursion. The only thing is that numIndex and operIndex doesnt control what char to look at. My program keeps skipping the math operation char and the ) char no matter what expression i input. I modified the function again, this time, it recognises the ) but it skips the 2nd number and the operator.
|
Author: | C14 [ Sat Nov 01, 2014 12:23 pm ] |
Post subject: | RE:java.lang.StackOverflowError? |
evaluate(s, numIndex, ++charIndex); reduce it down to one recursive call. Recursion has an 'unwinding' effect. The number is complete when you hit an operator or a right parenthesis. That's when the number should be stored. Usually loops are replaced with recursion. |
Author: | a22asin [ Sat Nov 01, 2014 12:29 pm ] |
Post subject: | Re: RE:java.lang.StackOverflowError? |
C14 @ Sat Nov 01, 2014 12:23 pm wrote: evaluate(s, numIndex, ++charIndex);
reduce it down to one recursive call. Recursion has an 'unwinding' effect. The number is complete when you hit an operator or a right parenthesis. That's when the number should be stored. . im not sure what u mean? |