aspose file tools*
The moose likes Beginning Java and the fly likes how to check if a value is integer in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to check if a value is integer in java" Watch "how to check if a value is integer in java" New topic
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: 1157

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: 10916
    
  12

have you checked the API to see if there is something that could help? I would strongly suggest looking at the Integer class...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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: 36514
    
  16
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: 7064
    
  16

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?
Artlicles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check if a value is integer in java
 
Similar Threads
problem in retreiving value
Problem with the Wrapper Conversion
How hashCode() works?
trouble with loops and invoking a method
String