Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.StackOverflowError  RSS feed

 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm trying to print this:

S(1) = 1
S(2) = 12
S(3) = 123
S(4) = 1234
......
S(9) = 123456789    
S(10) = 12345678910  
S(11) = 1234567891011
......

When I run the program I get the exception mentioned in the subject.
How do I get rid of the exception?
Thanks.

Here is my code:

 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step through your numbersequence method and assume a connumber that is > 1.
Can you see a way for the code to exit?

Also, when you need help with an exception it helps us if you post the exception and stack trace.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindsey Brooks wrote:Hello,
I'm trying to print this:
......
S(11) = 1234567891011
.......
You cannot print that as an int because it has so many digits it will overflow the range of permissible values. You will probably not get further than S(9).
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's a stack trace?
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Step through your numbersequence method and assume a connumber that is > 1.
Can you see a way for the code to exit?


How do I stop it from being endless?
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My output turned out to be:

n = 4
S(1)=1
S(2)=12
S(3)=13
S(4)=14
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much Swastik.
Here is another question: When recursion is used in a program does it mean that it's endless?
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are most welcome.  Recursion does not mean endless, but it does need an exit point. 
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindsey Brooks wrote:Here is another question: When recursion is used in a program does it mean that it's endless?

Recursion will be endless(ends with error) if an appropriate condition is not provided to end it which is not desirable.
You do not want your program to throw StackOverFlowError.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How do I make this recursive and at the same time end after n times?





 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one way, there could be other ways too.

 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindsey Brooks wrote:What's a stack trace?


This is:

Exception in thread "main" java.lang.StackOverflowError
at stuff.TestStuff.recurse(TestStuff.java:22)
at stuff.TestStuff.recurse(TestStuff.java:22)
at stuff.TestStuff.recurse(TestStuff.java:22)
at stuff.TestStuff.recurse(TestStuff.java:22)
(etc etc)

It tells us exactly what error occurred and where, as well as the call chain so we can trace the error up through the code if need be.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik,

I don't think it's a good idea to give out complete solutions in Beginning Java.  I know this is a bit of a judgement call, but in this situation the problem could be homework.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I turn this into recursive:



The outcome should be this:
S(1) = 1
S(2) = 12
S(3) = 123
S(4) = 1234
......
S(9) = 123456789       // length is 9
S(10) = 12345678910    // length is 11
S(11) = 1234567891011  // length is 13
......
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the two classes shown in these two threads were identical, I have merged them into one thread. The same answer will apply to both.

Please show us what progress has been made since the code shown was written.
 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!
Thanks Campbell.
 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Once again Thank You Very Much Swastik!
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!