• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check if a value is integer in java

 
Deepshan Shetty
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you paste your code and explain what exactly is it that you are having a problem with? Use code tags.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you checked the API to see if there is something that could help? I would strongly suggest looking at the Integer class...
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic