File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Add an exception for divide by zero to my fraction class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Add an exception for divide by zero to my fraction class" Watch "Add an exception for divide by zero to my fraction class" New topic
Author

Add an exception for divide by zero to my fraction class

Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
I'm trying to add an exception so when the user inputs 0 for the denominator, it throws the exception, and then asks the user for a new integer (thus starting the process over).

this is what my professor provided me with for a sample output...

--------------------Configuration: <Default>--------------------
Please enter the numerator: 1
Please enter the denominator: 0
Denominator cannot be zero.
Please enter the numerator:

Note, this must be handled by DenominatorIsZeroException.java.

The error I get is this....


--------------------Configuration: <Default>--------------------
F:\Object Java\Assignments\Assignment 3\.Java files\FractionTest.java:45: error: cannot find symbol
if (Fraction1.equals(fractionTest))
^
symbol: variable fractionTest
location: class FractionTest
F:\Object Java\Assignments\Assignment 3\.Java files\FractionTest.java:45: error: cannot find symbol
if (Fraction1.equals(fractionTest))
^
symbol: variable Fraction1
location: class FractionTest
2 errors

Process completed.

I'm pretty sure this error has to do with creating the new objects inside the try catch....





Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

Hi Luke,

You have unresolved compilation problems.
It's because you declared Fraction1 and fractionTest inside try-catch blocks and they are not available outside these scopes.

Try declaring them outside the try's scope.

e.g.



Cheers,
Claudiu

p.s. also, check the java naming conventions for variables here:
http://www.oracle.com/technetwork/java/codeconventions-135099.html#367
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
So far, everything works, but I am having a problem restarting after the exception happens. Right now, if someone enters a zero, the program moves on to entering an integer for nTest, dTest, but I want it to restart. I know I have to use a boolean 'while this is true, reset and ask for new numerator/denominator'...but I am having trouble writing the code. Can anyone help?



Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

Take a look at break and continue statements. As the code stands the way you've posted it, it just prints the error and moves on.

http://download.oracle.com/javase/tutorial/java/nutsandbolts/branch.html


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
I still can't figure out how to restart the while loop if a zero is entered and how to exit and continue with the program if a zero is not entered. I've read about break and continue statements, looked at the different loops, and I still cannot figure this out. Any help would be appreciated.

Thanks.

Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
I'm still stuck! I cannot see what I am doing wrong. Can anyone shed some light and point me in the right direction?
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

Two points:

1. The code at line 21 (inside the while loop) will never be executed since i is always false.
2. Look at line 49, you're not doing anything (restarting) when that fraction is invalid.

Both break and continue statements skip the rest of the loop. The difference between them is that break exits the loop (no restart) while continue goes back to the beginning. Try taking out your while loop at line 20 and see what happens.
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
Ugh...I see what I did now. Thank you kindly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add an exception for divide by zero to my fraction class