Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SMART way to match [0.997u -9.978u .98u -.987u ]

 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the following input ...[0.997u -9.978u .98u -.987u ]
do u have any SMART way to match it ..
the four cases in the list are the ONLY valid inputs
anyideas ??!!
 
fdafd fdafda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this is a SMART way of matching the number, but it should work. As the strings are relatively short, it won't matter much even if the pattern matching is inefficient

"[-]?[0-9]*.[0-9]+u"
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean only those four exact sequences are valid? If so, the easiest way to validate the input is with a simple comparison:
If those are just examples, how strictly does the input have to conform to them? Does there have to be zero or one digit before the decimal point, and two or three after?
 
Jean-Sebastien Abella
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the recipe!
1.trim the string
2.remove the first and last character as they are [ and ]
3.use a string tokenizer with the space as a separator( I think it is the default)
4.For each token string
4.1 trim
4.2 Remove the last character because it is the letter u
4.3 parse double with Double class.
4.4 insert that double in a list or whatever you need

that's it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic