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
Solved

Some Integer check method

Posted on 2006-07-21
9
198 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …

839 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