• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

check if is a number

 
Cathy Young
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have some strings and need to check if some of them are numbers. I am wondering if there is any existing API or implementation for this.
I thought of two ways to do it, but was not happy with either of them.
1. check the string character by character to see if it is a number. -- this is too boring and inefficient.
2. try Integer.parseInt (String) or Float.parseFloat (String) to see if NumberFormatException is thrown. I am worrying if there is any potential problem and it is too expensive to do this.
Any solutions or suggestions?
Thanks,
Cathy
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(2) is the way I do it.
hth,
bear
 
Cathy Young
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bear!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic