Solved

Some Integer check method

Posted on 2006-07-21
9
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 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