Re: test if a string is an integer?

From: Raymond DeCampo (rdecampo_at_spam.twcny.spam.rr.spam.com.spam)
Date: 07/16/04

  • Next message: dave: "Exception in thread "main" java.lang.NoClassDefFoundError"
    Date: Fri, 16 Jul 2004 12:23:52 GMT
    
    

    Chris Dutton wrote:
    > As an alternative to the solution already provided...
    >
    > import java.util.regex.*;
    >
    > ...
    >
    > Pattern integerPattern = Pattern.compile("^\d*$");
    > Matcher matchesInteger = integerPattern.matcher(myString);
    > boolean isInteger = matchesInteger.matches();
    >
    > or:
    >
    > boolean isInteger = Pattern.matches("^\d*$", myString);

    Note that this method is *not* equivalent to the other methods which use
    Integer.parseInt(). This method will accept inputs that are outside the
    range of a Java int, while Integer.parseInt() will not. Use whichever
    one is appropriate for your application.

    Also, I believe you forgot about the negative sign. :)

    HTH,
    Ray

    -- 
    XML is the programmer's duct tape.
    

  • Next message: dave: "Exception in thread "main" java.lang.NoClassDefFoundError"

    Relevant Pages