Results 1 to 8 of 8
- 02-09-2011, 04:37 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
- 02-09-2011, 05:01 PM #2
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.
- 02-09-2011, 05:04 PM #3
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!
- 02-09-2011, 05:16 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Build a wall around Donald Trump; I'll pay for it.
- 02-09-2011, 05:18 PM #5
Good point! I took the OP's
string contains only numbers
- 02-09-2011, 05:38 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 02-09-2011, 07:26 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
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?
- 02-09-2011, 07:32 PM #8
Similar Threads
-
Recursive method that checks if the first n elements of an array is sorted?
By matrixcool in forum New To JavaReplies: 8Last Post: 01-15-2011, 10:39 PM -
Error: "The method startsWith(String, String) is undefined for the type StringUtils"
By shihad_s in forum New To JavaReplies: 10Last Post: 12-07-2010, 01:29 PM -
JXL, Numeric data issue
By only2008 in forum Java ServletReplies: 0Last Post: 02-27-2009, 12:36 PM -
Java program that checks in which quadrant a given point belongs
By ningning860310 in forum New To JavaReplies: 5Last Post: 09-09-2008, 05:37 PM -
get numeric value from a text field
By Lehane_9 in forum New To JavaReplies: 2Last Post: 06-14-2008, 04:19 AM
Bookmarks