aspose file tools*
The moose likes Beginning Java and the fly likes new error message help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new error message help?" Watch "new error message help?" New topic
Author

new error message help?

Daniel Brackett
Greenhorn

Joined: Dec 09, 2011
Posts: 8

Hi all,

I am lacking understand as to why I am getting this error message
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)

I get the message about 1000 times any ideas?


Here's the Tester class


Learning Java on a Ubuntu Linux 10.04 Machine
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 560
Because you can't see the first line before the first 10K times the error occurred. It's probably a stack overflow due to a recursive method that calls itself so many times that the stack overflows. Check the exit path of your recursive algorithm to see why it might not be executing.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11318
    
  16

or if you want to see the whole thing, redirect the output to a file, then look at that.

Another option would be to run it for a smaller input...say 20, and see what that does.

Greg is correct..Here is what i get at the top (my number is different since I just put all your code in one file):



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Daniel Brackett
Greenhorn

Joined: Dec 09, 2011
Posts: 8

[quote=Greg Brannon
It's probably a stack overflow due to a recursive method that calls itself so many times that the stack overflows. Check the exit path of your recursive algorithm to see why it might not be executing.

How does one check the exit path? what is the exit path?

I just want the output on the console. This is possible no?

So I could just rewrite it as a for loop Yes?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Daniel Brackett wrote:How does one check the exit path? what is the exit path?


Whenever you have a recursive method - one where the method calls itself - there must always be at least one situation where it doesn't call itself. And that must be a situation that will eventually happen. Otherwise you get an infinite loop, which is what happened to you.

The divideBy5() method that you've written always calls itself again. There's no exit condition: once it's called, there are no circumstances where it will ever end.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11318
    
  16

"check the exit path" means "look at the code and see what it is doing - how does your code break out of the recursive call?"

So let's say you go into your divideBy5 method, and val does equal 0. What happens? you call maketotal() (by the way, this really does nothing), decrement val, and then you call divideBy5. Val will now be -1. you call makeTotal, decrement val, etc...

So assuming you had enough memory, EVENTUALLY you would get to the lower limit of val, it would roll over to a positive value, and then run through a different branch of divideBy5....

but there is no way to ever get out of your recursive call.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 560
How does one check the exit path? what is the exit path?

I was going to ask you the same questions, except I might have led off with, "Is there an exit path?"

A recursive algorithm usually identifies a "base case" and tests for that during execution of the recursive method. Once the base case is achieved, then the recursive algorithm exits rather than calling itself (recursing) and 'unwinds'. So when should your divideBy5() method stop? What's the base case or indication that it doesn't need to continue calling itself.
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
Hi,

The recursion is never termianted.

I think you should check your else-statement. Seems like the curly brackets are missing, which means the divideBy5() is called even if val<=0 is true.

I hope taht helps!

Michael
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
by the way, is it an exercise to practise recursion?

Otherwise, I believe that a single for-loop would do the job and the code would be easier to understand an debug. :-)

Cheers,
Michael
Mihai Boisteanu
Greenhorn

Joined: Jul 10, 2012
Posts: 8

Here is the problem:


You need to add some brackets there because after you test those you always invoke that function again, and again, and again until you get bored to death. So just use some brackets like here and you will be ok:
Daniel Brackett
Greenhorn

Joined: Dec 09, 2011
Posts: 8

Michael Krimgen wrote:by the way, is it an exercise to practise recursion?

Otherwise, I believe that a single for-loop would do the job and the code would be easier to understand an debug. :-)

Cheers,
Michael


OK I need to review for loops before I do that. I started with recursion because it seemed right.

I would like to be able to do it recursively as-well I guess that I'll set up two different methods to do the same thing. good practice for next year.

Still havin' a rough go at it here's what doing recently.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11318
    
  16

it seems like you are putting the cart before the horse. The right way to do it is to think through the problem first, in English, and actually write out the steps. This seems VERY complicated. I don't understand why you need to use recursion AT ALL.

So...tell us your algorithm. What are you trying to do, and why do you need to do it recursively?
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
how about



I don't want to do your work here, but what you want seems to be too simple to discuss for hours ...

Cheers,
Michael
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Challenge: Now do it without using % 5. You only need one % operator. You do need two loops, but miss out most of the % tests which will come out false.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11318
    
  16

Michael Krimgen wrote:how about



I don't want to do your work here...


Please DON'T do their work. We encourage people to figure this out for themselves. Providing a full blown solution doesn't really help anyone.
Daniel Brackett
Greenhorn

Joined: Dec 09, 2011
Posts: 8

Ok so It's over.
Here is the solutions both in Recursive and For Loop variants.
Why recursive? because practice makes me better and I have only been at programming for about 9 months now. Thinking about different solutions to problems is good practice to understanding how the problem could be resolved.



and

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Let’s see a version not using % 5. It can be done; it took me ages. Well over three minutes.
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
Campbell Ritchie wrote:Challenge: Now do it without using % 5. You only need one % operator. You do need two loops, but miss out most of the % tests which will come out false.


Next challenge would be to have no % operator and no loop. Took me about 2 minutes. :-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Not bad. I shall have to think about that one. Is it a version of the sum of an arithmetical progression?
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
Campbell Ritchie wrote:Not bad. I shall have to think about that one. Is it a version of the sum of an arithmetical progression?


It's a simple modified version of the 1 + 2 + 3 ... + n formula. Would be much more performant for really big numbers (using BigInteger). Imagine loops i case of n = 10^20 ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Yes, 1 + 2 + 3 +...+ n is an arithmetical progression, as is 3 + 9 + 12 ... + n. What do you do about duplicates, eg 15?
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
Campbell Ritchie wrote:Yes, 1 + 2 + 3 +...+ n is an arithmetical progression, as is 3 + 9 + 12 ... + n. What do you do about duplicates, eg 15?


first calculate 3 + 6 + 9 + ...
then caculate 5 + 10 + 15 ...
add the two results
then subtract 15 + 30 + 45 ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
It’s so easy and obvious when it is pointed out. Why don’t we have a smilie with Homer Simpson saying D’oh?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11318
    
  16

Daniel Brackett wrote:


There is some unnecessary stuff here. do you really need an "else if"?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new error message help?