• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Best Way to Extract Numeric Value From a String  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My string: 95.0 LBS. MAX WEIGHT

I need to extract the 95.0 out of string.

My code that does not work in that it gives error that there are two decimal points

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe that way satisfies your requirement?
 
Steve Dyke
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having some issues:

The following throws the InputMisMatchException

Note String is: WEIGHT 153.0 LBS MAX

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know what separates the tokens in the String and the position of the token with the numeric value in the string of tokens, you could split the String and access the desired token in the returned array.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Define what is a "Numeric value".
2. Define all possible String patterns.

And post here your answers. Otherwise gets too difficult for everyone to make an assumption what your requirements are.
 
Steve Dyke
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:If you know what separates the tokens in the String and the position of the token with the numeric value in the string of tokens, you could split the String and access the desired token in the returned array.


Thank you for the advise.

Help me to understand, Scanner only parses the first token of the string to be scanned?

So '95.0 Lbs' will work but not 'Weight 95.0 lbs'
 
Steve Dyke
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:1. Define what is a "Numeric value".
2. Define all possible String patterns.

And post here your answers. Otherwise gets too difficult for everyone to make an assumption what your requirements are.


WEIGHT 153.0 LBS MAX

Numeric value would be 153.0

95.0 LBS MAX

Numeric value 95.0

Patterns xxxx.x, xxx.x, xx.x, x.x
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:WEIGHT 153.0 LBS MAX

Numeric value would be 153.0
That is obvious. No need to explain such.

How about:
WEIGHT 144 LBS MAX?

Can be like that?
 
Steve Dyke
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Steve Dyke wrote:

How about:
WEIGHT 144 LBS MAX?

Can be like that?


Yes.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:Patterns xxxx.x, xxx.x, xx.x, x.x
What does that mean then? It seems like only decimal numeric values patterns here specified, but now you just confirmed that "144" is a valid numeric value too, which isn't listed in the patterns you posted earlier.
 
Steve Dyke
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Steve Dyke wrote:Patterns xxxx.x, xxx.x, xx.x, x.x
What does that mean then? It seems like only decimal numeric values patterns here specified, but now you just confirmed that "144" is a valid numeric value too, which isn't listed in the patterns you posted earlier.


Sorry, just forgot that possibility.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:
Patterns xxxx.x, xxx.x, xx.x, x.x


Well, since all decimal points *must* have a digit following it, I guess the original issue can be fixed by also replacing the decimal points that fail this test too.

Something like this (which I have not tested)...


Now, of course, the next question would be, what should happen if the string has two valid numbers. This technique would merge it into one invalid number.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you'll need to use regular expressions. Check this google search results. Using help of top 3 links you can come up with a solution. Show us later what you got, so somebody could have a look.
 
Steve Dyke
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Probably you'll need to use regular expressions. Check this google search results. Using help of top 3 links you can come up with a solution. Show us later what you got, so somebody could have a look.


Thanks, I have went with Henry's suggestion.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you have a regular expression, try this Scanner method. Beware: it returns null if nothing is found.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!