Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

checking variables  RSS feed

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it valid to test a string is a number by catching an exception?

i.e.

String s = "blah";
try { //test if string is a number
Integer i = new Integer(s);
} catch (NumberFormatException nfe) {
System.out.println("s is not a number";
}
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is of course valid,
but I propose using regular expressions for this
(s is the string u want checked for numeric-ness):

s.matches("\\p{Digit}++")

give u true if s is a number, and false else.
Good luck
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!