This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add an exception for divide by zero to my fraction class

 
Luke Stamper
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Luke Stamper
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh...I see what I did now. Thank you kindly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic