Solved

Some Integer check method

Posted on 2006-07-21
9
195 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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

12 Experts available now in Live!

Get 1:1 Help Now