• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to check if a value is integer in java

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic