• Post Reply Bookmark Topic Watch Topic
  • New Topic

Float Validation  RSS feed

 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the columns of my JTable takes a String that has to represent a float such as 5.1 or 2.1 I have searched the formes for a way to valadate this and tried a few things such as




/// audio is the String //// which did not throw an Exception whern I typed in 66 or 99 or 333.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integers are valid floats, too. Do you mean to insist that the number contain a decimal point? You might then try checking 'audio.indexOf(".") != -1' .
 
Campbell Ritchie
Marshal
Posts: 56209
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try looking for a regular expression which will match a float. Remember that there are limits to the range of a float, beyond which a valid number eg 1.234e56f may throw a NumberFormatException.

I have see an Java™ grammar which includes examples of regular expressions for Java™ number literals. I can't seem to find it now, I am afraid. Sorry. I did find this on Google, however.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As usual, you are brilliant. this works:


 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if audio's value is something like "www.coderanch.com"? That includes dots as well.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I meant the indexOf() check in addition to the parse check, not instead of.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!