Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Try/Catch InputMismatchException not working.  RSS feed

 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings everyone. So I have written a program that is supposed to figure the ratio of two numbers. It is supposed to warn if the 2nd number is zero and also catch if the user inputs something other than a number.  Everything appears to be working except the Catch for the InputMismatchException.  Only a few weeks into Java so still pretty much a noob. Any pointers on how to fix this or where my code is incorrect would be appreciated.  Side note: I know my code is a little bit of a mess at the moment. I usually go thru at the end and fix the formatting. Thanks again for any pointers!

My code:



Current output when attempting to enter something other than a number:
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rebecca Berry wrote:. . . I usually go thru at the end and fix the formatting.  . . .
Very bad idea. If anything goes wrong before that, in unformatted code, it will be much more difficult for you to read the code and work out what has gone wrong. You should be able to take your glasses off/try a friend's glasses, so you can't read a single work of the code, and still see the structure of the code from its indentation. We have all sorts of advice about editors, and usually advise people not to use IDEs, but you seem to be using NetBeans, so why not get NetBeans to do all the formatting for you. You can probably do it with about two keystrokes for the whole class.
One of the things I think you sh‍ould never do is to put { and } on the same line (except in array initialisers). See where you have done that. See whether that explains your current problem.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rebecca Berry wrote:  Everything appears to be working except the Catch for the InputMismatchException.


Considering that there is zero code in the try block, I highly doubt that the catch block will execute.  The code in the catch block is only to catch exceptions thrown from the try block -- and the exception that you shown (stacktrace) certainly doesn't originate from there.

Henry
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't catch Input mismatch exceptions. There is never any need. You can always avoid that Exception. The following trick which Rob Spoor taught me ages ago works for keyboard input of any of the specific types which Scanner reads. The loop shou‍ld ideally be in its own method, and that method is ideally in its own class, but this is what the loop looks like. The message would be in a format like, ”Please enter how many entrants: ”You can swap the order of lines 6 and 7. You can read what sort of XYZ a Scanner can read in its list of methods.
 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I think I fixed it.  All seems to be compiling the way it should be.  Thanks again everyone for the pointers!  When you are essentially self teaching yourself friendly help is always greatly appreciated.  Until my next question...here is my working code.



Zero Exceptions Class
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations on getting you program working!  If you want a few more pointers to improve the code, read on.

Ingeneral, the main method should have no more than two lines in it, like this:

Then put all of your code from main() into a method called run().  This may seem silly at first, but in the "real world", very little is ever in main().  It also helps when you find you have to make everything static.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid I don't like to see throw and catch in the same place. If you create an Exception and throw it and catch it in the same method, all you achieve is a very inefficient way of implementing an if‑else. If the Exception comes from elsewhere, then it is correct to use try‑catch.
You know you can divide a double by 0? You get ±∞ or that strange value NaN. You can read about them in the Java┬« Language Specification (=JLS). Beware: the JLS can be difficult to understand.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to enhance my getXYZ method from yesterday with a getXYZNotZero method, which will ask for a new input if you enter zero.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!