Solved

Some Integer check method

Posted on 2006-07-21
9
196 Views
Last Modified: 2010-03-31
Hey experts,

 Is there any java built-in method similar to VB's IsNumeric method; I need a methd that checks if a string variable is an integer or no. I know I can do it by trying to instantiate a new object of class Integer, and an exception would occur if it's not an integer, but I am trying to see if there is a better way.....any help??
0
Comment
Question by:mte01
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17155581
Long.parseLong(x)

will throw an Exception if not a number
0
 
LVL 3

Author Comment

by:mte01
ID: 17155628
>>CEHJ

I am trying to see something better than having to check for an exception....the Integer.getInteger method seems good enough....
0
 
LVL 2

Assisted Solution

by:RoyalNepal
RoyalNepal earned 50 total points
ID: 17155812
No...there is no such built in function in java...

Two ways:

1)Throws an exception if its not a number..
2) write a function...

//Here is the code for that.....

private static final boolean isNumeric(final String s) {
  final char[] numbers = s.toCharArray();
  for (int x = 0; x < numbers.length; x++) {      
    final char c = numbers[x];
    if ((c >= '0') && (c <= '9')) continue;
    return false; // invalid
  }
  return true; // valid
}

Cheers.

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 17156159
An exception-free method would be

if (s.matches("\\d+"))
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:mte01
ID: 17156609
>>CEHJ

Great..that'll do the trick...thanks for your help!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17156728
:-)
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17156930
i think it should be ^\\d+, simply \\d going to return true if there is a number
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17157056
>>simply \\d going to return true if there is a number

No. Returns true if *only* a number
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17157084
agree

confused with find method
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now