| Author |
how to check if a value is integer in java
|
Deepshan Shetty
Greenhorn
Joined: May 28, 2012
Posts: 28
|
|
In my code I'm trying to test if a value is an integer or not. If the entered value is an integer, than I let the user proceed; However, If it isn't the user then the code should return a false string.
can anyone help me with a java code for it?
|
 |
Mansukhdeep Thind
Ranch Hand
Joined: Jul 27, 2010
Posts: 1142
|
|
|
Can you paste your code and explain what exactly is it that you are having a problem with? Use code tags.
|
~ Mansukh
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
|
have you checked the API to see if there is something that could help? I would strongly suggest looking at the Integer class...
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
Do you mean an integer in the code, or an integer entered from the keyboard?
If the latter, then there are some useful things in the Scanner class. You may find something useful in the Java Tutorials, as well as the Scanner documentation.
When I was starting Java, we all wrote our own utility classes for keyboard input. It is probably still worth writing such classes nowadays, only Scanner has made it easier. Start looking here.
|
 |
Deepshan Shetty
Greenhorn
Joined: May 28, 2012
Posts: 28
|
|
I don't have a code for it. I just want to check if a value (from keyboard) is integer or not. Just a simple code.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
So the Scanner methods don’t help?
What about a regular expression? You should be able to design a regex to match integers quite easily. Note that will not necessarily match an int.
|
 |
Balaguru Gupta
Greenhorn
Joined: Mar 03, 2013
Posts: 10
|
|
Hello Deepshan Shetty,
I hope this code will be helpful for you
/**
* @since @author balaguru
*/
public class IntegerValidator
{
public static void main(String[] args)
{
String value = null;
Scanner scanner = new Scanner(System.in);
boolean flag = false;
do
{
System.out.println("Enter value : ");
value = scanner.next();
try
{
Integer.parseInt(value);
flag = true;
System.out.println("Valid");
}
catch (Exception ex)
{
System.out.println("Not an integer, Invalid");
}
}
while (!flag);
}
}
what i have done is, i tired parsing the obtained value from the user.
If an exception arises, validation message is shown and asking the user to enter the value again.
Program will not be terminated unless the user enters the right value.
Cheerz mate
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4726
|
|
Balaguru Gupta wrote:I hope this code will be helpful for you...
Balaguru,
Please don't hand out ready-made solutions. I'm sure you were just trying to help, but we much prefer people to work things out for themselves.
Also, if you're posting code, please read the UseCodeTags (←click) page first. And read it carefully.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
I was about to delete your code (please read this), but I am going to leave it as an example of a very bad solution to the problem.
|
 |
 |
|
|
subject: how to check if a value is integer in java
|
|
|