This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Preventing Divide by zero exception

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to prevent against the "Divide by Zero Exception". This code just locks up the GUI of my program for some reason :



Goal 1 : Have a whole number division.
Goal 2 : Avoid the "Divide by 0 exception".

Thanks,
John Price
 
John Jai
Rancher
Posts: 1776
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check that the condition checking for one and two to be zero inside the while (the while with the '%' operator) will never be satisfied since you have already checked that in the preceding two while loops..

One solution might be to use do-while

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted it in my code and tried it out. It now works beautifully. Now, I am trying to understand it. I guess I'm wondering how this is different from my code... They look exactly the same to me, except for the do instead of the brackets (even though the brackets are there). How does this work differently than my previous code? Does the inside execute first?

Thanks for the help,
John Price

EDIT : "A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time."

^^ Does this have anything to do with it?

EDIT (2) : So, based on my own observations and from the quote above, I'm understanding that the do-while statements execute each time before the main while statement does. This is why it succeeds.
 
John Jai
Rancher
Posts: 1776
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The do while guarantees to run at least once and hence the one and two are assigned a new value till one gets perfectly divided by two (one % two == 0). Please try to print the values of one and two after the while loop finishes. Since you previously checked whether one and two are zeros in a while loop, which is already been checked the program falls into an infinite loop as the values of one and two never changes.

do-while statements execute each time before the main while statement does

I did not understand the above question... which is a main while? The one which comes with do?
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand now, thank you! By the "main" while, I meant :


The "head" while or whatever you want to call it. Thank you for the clear and extended explanation!

John Price
 
John Jai
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to check that the result is not 0? You can get a non-0 value from a Random like this
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*Slaps self* Thanks for the last post. I haven't used java.util.Random in forever and have forgotten how to use it I guess. Thank you very much for the reminder.

John Price

EDIT :
My new code :
 
John Jai
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john price wrote:*Slaps self and john *

Thanks Campbell
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:
john price wrote:*Slaps self and john *

Thanks Campbell


I'm glad you posted the other one. I've never used a do-while loop and never knew why it was needed. While Campbell's answer was more relevant to my question, I thank you for showing me this because A. you did give me the right answer and B. you taught me something new that otherwise, I probably wouldn't have learned.

Thanks,
John Price
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That shows how useful forum discussion can be. You get lots of useful ideas, even if they are tangential to your original question.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic