programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Bear Bibeault
• Liutauras Vilda
• Devaka Cooray
Sheriffs:
• Knute Snortum
• Junilu Lacar
• Henry Wong
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• Tim Holloway
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

# problem with division

Greenhorn
Posts: 6
Hello,
Can you help here, what would be the problem , I keep getting an  error  message that I cannot divide by 0..

Sheriff
Posts: 6801
182
Welcome to the Ranch, Silvia Santana!

We don't have enough information to tell what's going on.  If seems that N2 is always 0, but why we can't tell.  Please post more of the method or the entire class if it's small.

Saloon Keeper
Posts: 11441
247
I imagine that the calculation of result4 causes Java itself to throw the exception before you perform the N2 == 0 check.

If this is the case, the solution is to perform the N2 == 0 check before you calculate result4.

Silvia Santana
Greenhorn
Posts: 6
Thanks, here it is. I tried putting result 4 in the "if" after the N2 == 0 and still got the same error.

Stephan van Hulst
Saloon Keeper
Posts: 11441
247
So what happens when you print N1, N2 and operator before you calculate result1? And what input are you entering?

Silvia Santana
Greenhorn
Posts: 6
I am trying
112
0
/
or
10
0
%

This is when I get the error.

When the input is
10
12
+
I get what I should get - 10 + 12 = 22 - even

Ranch Hand
Posts: 116
11
Hi,

From the examples you posted, I think the error is expected because you're trying to divide by zero (for example: 112 0 / means 112/0 right?)

Try some input like:
112
2
/

And see if that works for you.

Stephan van Hulst
Saloon Keeper
Posts: 11441
247
Uhh, well what do you expect as the output when you enter
?

Saloon Keeper
Posts: 6921
65
Your problem is the error is being generated when line 14 is executed and you are not doing your divide by zero check till line 41.

Silvia Santana
Greenhorn
Posts: 6
Thank you all, the assignment says that I should be able to print Cannot divide by zero, and this is the example input I have (112 0 /).
I guess I will keep trying.

Zachary Griggs
Ranch Hand
Posts: 116
11
It sounds like you have met the requirements of the assignment - when you try to divide by zero, it prints out an error message saying you can't divide by zero.
(Edit) - I missed that you were dividing before as a test to see what the result was

Silvia Santana
Greenhorn
Posts: 6

Carey Brown wrote:Your problem is the error is being generated when line 14 is executed and you are not doing your divide by zero check till line 41.

I just saw  your message. What do you suggest I do? I tried moving line 14 after N2==0 but it does not work .

Zachary Griggs
Ranch Hand
Posts: 116
11
You probably just need to remove those variables (the result1-result5)
You can do the operation requested in the if statements. There is no need to do them all up front.

Silvia Santana
Greenhorn
Posts: 6

Zachary Griggs wrote:You probably just need to remove those variables (the result1-result5)
You can do the operation requested in the if statements. There is no need to do them all up front.

Yay, it worked! Thanks a lot. I guess I still don't understand why it matters for all the variables to be in the ifs, but obviously it does make a difference.

Zachary Griggs
Ranch Hand
Posts: 116
11
• 1
The difference is that you were trying to divide a number by N2 before you checked if N2 was equal to zero or not. This crashed the program if it was equal to zero. Doing the math inside the if statements is better practice regardless since doing them up-front leads to a lot of unnecessary math.

 My honeysuckle is blooming this year! Now to fertilize this tiny ad: Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java