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.
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.
Joined: May 28, 2012
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.
Joined: Oct 13, 2005
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.
* @since @author balaguru
public class IntegerValidator
public static void main(String args)
String value = null;
Scanner scanner = new Scanner(System.in);
boolean flag = false;
System.out.println("Enter value : ");
value = scanner.next();
flag = true;
catch (Exception ex)
System.out.println("Not an integer, Invalid");
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.