• Post Reply Bookmark Topic Watch Topic
  • New Topic

Check for integer input  RSS feed

 
Sohail Hasware
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Nice, thanks
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!