Preventing Divide by zero exception

john price
Ranch Hand
Posts: 495
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
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
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
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
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
You are welcome

Campbell Ritchie
Sheriff
Posts: 50660
83
• 3
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
*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
john price wrote:*Slaps self and john *

Thanks Campbell

Campbell Ritchie
Sheriff
Posts: 50660
83
You’re welcome

john price
Ranch Hand
Posts: 495
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
Posts: 50660
83
That shows how useful forum discussion can be. You get lots of useful ideas, even if they are tangential to your original question.