Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

checking if String is numeric  RSS feed

 
christine clarin
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, i have a function which returns a string. i want to check if the string contains a numeric value. how do i do it? example:

"16" -> i want to know that "16" contains a numeric value as opposed to "done" thanks!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.parseInt will throw NumberFormatException if it's not a number
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Number != integer. For the purposes of your application, what kind of patterns do you consider numbers christine clarin?
 
christine clarin
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for my application, numbers would be anything from 0 to 1000...any number/integer actually.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any Integer? Then Satou kurinosuke's solution will work fine. Any number and you'll need to watch for other things.
[ June 09, 2006: Message edited by: Paul Sturrock ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for pointing this out
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you could say

boolean isNumber = myString.matches("\\d+");

then you don't have the overhead of having a new exception constructed if the test fails.
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This function will evaluate to true if myString contains only digits, and false otherwise
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!