Results 1 to 8 of 8
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default A String method that checks to see the data is numeric?

    I have a textfield in which the user is supposed to enter a number. I want to check to check the field to make sure the string contains only numbers. is there a function that can do that?

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    The easiest way i can think of would be to use String's "matches" method. Then you can check the string against some regular expression to see if there are only digits. I'm not amazing with regex, but the bottom post here might be what you're looking for.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Yeah, simply try Long.parseLong() on the text they entered (you'll need a try catch). If an exception is thrown, then there was something other than a number and you can notify the user. If no exception is thrown, its a number. This will work on anything up to Long.MAX_VALUE.
    If you're dealing with much larger numbers, then you might try using a simple regex inside the String.contains() method. You could search for non-number types like [a-zA-Z\s] and symbols. If a match is found, then there are non-numbers in the string. Good Luck!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    Yeah, simply try Long.parseLong() on the text they entered (you'll need a try catch). If an exception is thrown, then there was something other than a number and you can notify the user. If no exception is thrown, its a number. This will work on anything up to Long.MAX_VALUE.
    What if the user wants to enter a floating point number? Just accepting large integral numbers won't do it. My suggestion is to use a X.parseX() method for X == Integer, Double or Long; the op should make a choice.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Good point! I took the OP's
    string contains only numbers
    to mean a string with only digit characters.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    Good point! I took the OP's to mean a string with only digit characters.
    Mwah, the String "42.54" contains a number too. The OP should've been more explicit w.r.t. numbers/digits.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Thanks, I've got the answer I wanted and sorry for not being specific. the number had to be an int - no decimal points!
    By the way, what does OP stand for?

  8. #8
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 01-15-2011, 09:39 PM
  2. Replies: 10
    Last Post: 12-07-2010, 12:29 PM
  3. JXL, Numeric data issue
    By only2008 in forum Java Servlet
    Replies: 0
    Last Post: 02-27-2009, 11:36 AM
  4. Replies: 5
    Last Post: 09-09-2008, 04:37 PM
  5. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •