Solved

Some Integer check method

Posted on 2006-07-21
9
197 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 86

Accepted Solution

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

if (s.matches("\\d+"))
0
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 73
backtracking recursion  code 19 58
GUI builder for Eclipse? 8 35
JavaFX TableView not displaying correctly 3 17
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

776 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