This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

strange input format  RSS feed

 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
i need help in validating the input of the user
the user is required to enter a list of numbers that can be decimal or number or number in micros
example [ .876 76.5 0.87u .79u 578u 0.786 ]

and i also need to validate another list where the input could be
[78 .99 1.2e-4 0.33e-6 1.13]

and i cant find a way to validate both lists
please help me

thanks is advance
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a StringTokenizer to chop the input string into the space-separated pieces, and then use regular expressions (java.util.regex package) to extract the actual numbers. If you're not familiar with regular expressions, that's something every developer should know - they simplify a number of tasks tremendously.
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do u mean to extract word by word and check if it matches one of the possible patterns
example
x.matches("[-]{0,1}\\d{0,}.\\d{1,}u{0,1}");
 
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
PLease don't post the same question to multiple forums. Because there's already a discussion going here, I deleted the other copy I found in Java in General (Intermediate.)
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, something like that. Or you can just check whether the string ends with 'u' and treat that as special case, and for everything else you use Double.valueOf().
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!