Hi all, so this question I believe is pretty simple. I am trying to come up with a solution to two parts in my code which create errors when I accidentally enter a non integer. It is at the PIN block of my code and the options block of my code. I have read about parseInt or InputMismatchException, but I can't seem to get either method to work in my code. Can someone help me create an exception so that when a non int is entered, if does not give an error message, it gives a message that that was not a int, and it requests for them to enter a integer? The two areas that I am trying to trow these exceptions is in lines 37-39 and 60-104 (Options area of the code) where integers have to be put in for the PIN number and the options selection. Here is my code:
I'm not sure that throwing an exception is your best choice here. Exceptions should be reserved for "exceptional" conditions. Improper input in user interfaces, though not desired, should be expected and should be handled with appropriate logic. Sometimes helper methods like this (see below) can make your code more concise and compartmentalize the error handling.
First, have a cow for a putted effort (a lot) to your app. It is clear that you are serious about what you're doing. Great.
Now about other stuff. I think you have a bit more problems with your code, but who knows, maybe for the start is not that bad. However, I'm going to tell my own insights, I'm almost sure there will be more people who'll find this topic interesting.
Your current code is very procedural. Also eerything is crammed to a one method which is called "main", which does no good for you as program becomes more complex and will be very difficult to follow what is going on here. Actually it is already. Also it seems you a bit misunderstanding what the objects are about.
I will definitely start working on trying to implement more methods into my program. In fact, that's what I've been trying to work on lately. However, one question regarding that is, would I make small sections of my code into methods? Or would I try and take an entire while loop and make it into a method. The only reason I ask is because I have scanners which are implemented into my loops, so is there a way to get these scanners to become part of a method? Or are my methods limited to the location of my scanners? For example if that wasn't clear, if in my first while loop, where I am directing what the program should do if the input is not correct, how should I go about making that a method? Would I make the three prints into one method and leave the while loop in the main method, since I have a counter outside of the loop as well as a scanner within it, or is there some way to make the whole thing a method? Scanner in all?
@Carey Brown. When I tried to implement your helper method into my tested class, it gave an error that nextInt() cannot be dereferenced. I believe I was not using your helper method correctly. This error, was among other errors. How would I use your helper method to help the programs logic identify between a int and what to do if a non int appears?
David Vach wrote:@Carey Brown. When I tried to implement your helper method into my tested class, it gave an error that nextInt() cannot be dereferenced. I believe I was not using your helper method correctly. This error, was among other errors. How would I use your helper method to help the programs logic identify between a int and what to do if a non int appears?
Ah, I left out
(edit) In my example I print out an error message and then prompt again. You can change it to behave however you want. Note that if there's no integer in the input you have to call scan.next() to absorb any leftover input characters.
@Liutauras Vilda Hi, so I made each important section of my code into methods, like you had suggested. Does this make it better and easier to understand/read? It works the same as my old code, which is not a problem. I still have the issue of it not making an exception for non integer inputs for the PIN and options, however.
Here is my revised code:
Carey Brown wrote:I'm not sure that throwing an exception is your best choice here. . . . Improper input in user interfaces, though not desired, should be expected and should be handled with appropriate logic. Sometimes helper methods like this . . . .
I prefer the version Rob Spoor taught me a long time ago:-I think that method belongs in a utility class. Lines 6 and 7 may be written in either order.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop