I am doing a project in class. We have to make a program that scans Two Postive Integers, one at a time, using try-catch block. Can some one please help me get started?
Kayla1993 0 Newbie Poster
Recommended Answers
Jump to Postit still doesn't catch, because you still don't throw an exception. the catch will only be executed, if there is an exception to be caught.
Jump to Post.Throw an error if the input number is less than zero.
You missed this part out. The catch is OK, but you don't throw the exception when the input is wrong.
Jump to PostI dont understand, How am I not throwing an exception?
eg
if (! ans.equals("y") || ans.equals("n")) throw new InputMismatchException("must be y or n");
Jump to PostYou may wish to decrement the number of attempts at some stage.
If you want to decrement it when you have received a valid input then you will need to make the if condition body a multi-statement one. (Your current condition does not enclose the statement in braces so therefore …
All 13 Replies
delta_frost 0 Newbie Poster
stultuske 1,116 Posting Maven Featured Poster
Kayla1993 0 Newbie Poster
stultuske 1,116 Posting Maven Featured Poster
Kayla1993 0 Newbie Poster
stultuske 1,116 Posting Maven Featured Poster
Kayla1993 0 Newbie Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Kayla1993 0 Newbie Poster
Kayla1993 0 Newbie Poster
Kayla1993 0 Newbie Poster
mlesniak 22 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.