This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

checking variables

 
Kris Reid
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";
}
 
Frank Verbruggen
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic