Results 1 to 3 of 3
Thread: Easiest/Best way to achieve this
- 12-19-2011, 05:35 AM #1
Easiest/Best way to achieve this
Hi, I am doing a Math game so obviously I am asking the user to input numeric based information. I wanted to know the best way to prevent a program from crashing if the user enters anything other than digits. For example, if I enter "L", the program crashes. I believe it has something to do with the scanner using nextInt();
Thank you in advance for any pointers you may have to prevent these errors. Would it be an if statement? Or something much more complicated?
Thank you for your time.
Regards,
NM.[A!B]Java
-
Re: Easiest/Best way to achieve this
Myself, I'd get the text as a String (if using a Scanner, then it's myScanner.nextLine()), and then parse the String to an int in a try/catch block, catching for a NumberFormatException. There are likely plenty of examples of this in this forum if you search. Start with the terms -- Scanner and NumberFormatException. Luck!
- 12-19-2011, 05:50 AM #3
Re: Easiest/Best way to achieve this
Thank you very much, I actually have a pdf version of a book and saw the try, catch example there. I weren't too sure if this was still used as the book was made in 2002. I'll have another look at that along with searching the forum.
Thanks for the tip.
Regards,
NM.[A!B]Java
Similar Threads
-
What is the best to achieve this sort of layouts ? (Newbie)
By terry_513 in forum JavaFXReplies: 0Last Post: 11-21-2011, 08:58 AM -
easiest ide to install
By ishango in forum New To JavaReplies: 5Last Post: 03-16-2011, 12:50 PM -
Easiest way to convert String to StringBuffer
By unideal in forum New To JavaReplies: 4Last Post: 12-13-2009, 12:43 PM -
What is the easiest and most efficient vision library...
By jakx12 in forum Advanced JavaReplies: 1Last Post: 08-13-2009, 08:10 PM -
[SOLVED] How to achieve this?
By jazz2k8 in forum New To JavaReplies: 14Last Post: 05-13-2008, 08:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks