• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

How would I throw an exception if an integer is not entered?

 
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Saloon Keeper
Posts: 8238
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Marshal
Posts: 7984
560
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

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.

1. First, have a look at this article -> MainIsAPain

2. Why you don't use more methods in your app? For instance:
  • printIntro()
  • showLoginScreen()
  • validatePIN()
  • ...
  • That way you'd find the way easier to follow the logic and deal with program's complexity. Would you like to try to decompose your program to a many small methods?

    3. Probably for a first step would be enough to accomplish 1st and 2nd steps and then we could think how to move forward. Don't want to overload you with excessive amount of comments.
     
    David Vach
    Ranch Hand
    Posts: 105
    3
    Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    David Vach
    Ranch Hand
    Posts: 105
    3
    Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @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?
     
    Carey Brown
    Saloon Keeper
    Posts: 8238
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    David Vach
    Ranch Hand
    Posts: 105
    3
    Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay, ill give that a try Carey.
     
    David Vach
    Ranch Hand
    Posts: 105
    3
    Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @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:

     
    Marshal
    Posts: 73019
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic