• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Check for integer input

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am a newbie to Java and have created my first java program that takes in input from a command line, sends this to a method with one argument to calculate the square root. This works fine. I want now to check if the input passed to the methods argument is a integer. I want the user to be able to send a string deliberately to cause a error so that I can catch it and gracefully generate my own custom error message. The following code is how the method looks like:




Thanks
-Sohail
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer.parseInt will throw a NumberFormatException if the string does not contain a parsable integer.
So put a catch for this and when it happens do the logic you want when the input is not a number.

Also, better use the Scanner class to get input from the user rather than that InputStreamReader.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Armitage wrote:So put a catch for this and when it happens do the logic you want when the input is not a number.


@Sohail: Also, you might find it easier to put it in a method, for example:
public int getValidNumber(BufferedReader in) {...

As for using Scanner, I wouldn't worry about it too much; you seem to have BufferedReader down (but I'm very biased; I loathe Scanner ).

Winston
 
Sohail Hasware
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the scanner class before but my loop never executed properly. I changed the code as you suggested, and it works, here is the changed code:



but the output when supplied with a string instead of a integer is the following:

Enter integer: gdf
ERROR: Only a integer is a valid input
The square root of 0 is 0
Do you want to continue? (y / n):



When the input is a string, I do not want the statement to be executed but jump over to the print statement of instead. How would some kind of if statement look like to check if this error has occurred and the jump over the

The square root of:

statement
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just put

inside the try soon after the parseInt and it will be skipped over if an exception happens because control would jump into the catch.
 
Sohail Hasware
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton, this works. I will review my code so that i myself understand the logic properly of try catch and do some more examples
 
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Java Tutorials. That link will tell you lots and lots about Exceptions. There is another way to check whether you are entering an integer, which only works with Scanner. I found one example, which uses floats, but you should be able to change that to use ints easily enough.
 
Sohail Hasware
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice, thanks
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic