Solved

checking string for numbers?

Posted on 2004-10-23
300 Views
Last Modified: 2010-03-31
is there a method that checks a string for only numbers? thanks in advance
0
Question by:kewel
    16 Comments
     
    LVL 49

    Accepted Solution

    by:
    Code below should do the trick:

    String onlynumber = "12345";

    if(onlynumber.matches("\\A([0-9]+)\\z"))
    {
      //only number
    }

    Regards

    -r-
    0
     
    LVL 33

    Assisted Solution

    by:hongjun
    try this

    public boolean isNumeric(String s){
      try{ Float.parseFloat(s); return true; } catch (NumberFormatException e){ return false; }
    }



    hongjun
    0
     
    LVL 3

    Assisted Solution

    by:aviadbd

    I would replace the pattern string to "\p{Digit}+", actually

    (that would make it, ' onlynumber.matches( "\p{Digit}+" ) ')

    AviadBD.
    0
     
    LVL 33

    Expert Comment

    by:hongjun
    my above function will check for floats and integers.
    For integer only,

    public boolean isNumeric(String s){
      try{ Integer.parseInteger(s); return true; } catch (NumberFormatException e){ return false; }
    }
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    hongjun, your function is in a way more sensitive to overflows than the pattern match is. Although this wouldn't be a problem to the request, it is a thing to consider.

    -r-
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    Go with Roonaan's solution, or someone elses. However, if you'd like to find out the positions in a string that contain a character (if any), then you could try this:

    //-----8<-------------------------------------------------------------------------------------------------\\
    String str = "cjfh3993uf93hf9h";
    char cArr = str.toCharrArray();

    String results = "";
    for ( int i=0; i<cArr.length; i++ )
        if ( Character.isLetter(cArr[i]) ) results += ", i";

    // Then output the results:
    System.out.println( "The following char positions in the specified string are no integers: \n" +
                                 results );
    //-----8<-------------------------------------------------------------------------------------------------\\

    HTH :)
    [r.D]
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Do you mean 'is there a method that verifies that a String should contain only numbers'?
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    If so, you could create one (if there's not already):

    boolean onlyInteger( String str ) {
       private char cTemp = str.toCharArray();
       for ( int i=0; i<cTemp.length; i++ )
           if ( !(Character.isDigit(cTemp[i])) ) return false;
       return true;
    }

    However, hongjun's method is pretty cool. :)

    [r.D]
    0
     
    LVL 86

    Assisted Solution

    by:CEHJ
    If so:

    boolean allDigits = s.matches("\\d+");
    0
     
    LVL 3

    Expert Comment

    by:aviadbd

    About containing only numbers, the regexp should do teh trick.

    Testing it against a "\p{Digit}+" does exactly that - Check that only digits occur in the string, without so much code.

    AviadBD.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>without so much code

    Still more than my example uses ;-)
    0
     
    LVL 3

    Expert Comment

    by:aviadbd

    Must agree with you on that  ;)

    Better than caughting exceptions all the time, though.

    Aviad
    0
     
    LVL 92

    Expert Comment

    by:objects
    hongjun's suggestion is best if you want to check if the string is a valid number.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>hongjun's suggestion is best if you want to check if the string is a valid number.

    No it isn't
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    If you define a "valid number" as "a numeric values within the boundary off a javascript int", it is.

    ;-)

    -r-
    0
     
    LVL 33

    Expert Comment

    by:hongjun
    points to hongjun
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: Foundations of Front-End Development

    Jump-start a lucrative career in front-end web development, with zero previous coding experience required. This course covers the basic programming concepts and languages required for creating engaging websites from scratch.

    Suggested Solutions

    Title # Comments Views Activity
    static class 3 36
    linearIn  challenge 23 38
    Jasper Report: Crosstab Report- Include Page Footer 2 22
    array11 challenge 16 20
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    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…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    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…

    877 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