Rick Hoving
Rick Hoving

Reputation: 3575

Check if a String is a number

I know you can check if a string is an integer by just doing Integer.parseInt("1234");
But what I want is to do is link a textual number to an integer.
i.e.

Is there some library I can use to do this, or do I have to program this all by hand?

The reason I want to do this, is that I have an android application with speech recognition.
The user can then count. This number is shown on the screen.

EDIT
After some experimenting, I figured out that the SpeechRecognizer class I use automatically parses numbers to actual numbers...

Upvotes: 7

Views: 389

Answers (2)

stinepike
stinepike

Reputation: 54682

As far I know there is no library for this but you can think like this

create three types of tokens

a. one, two, three.................... nine

b. eleven to nineteen, twenty, thirty , ............ ninety

c. hundred, thousand, ........ and bigger values

now parse your input string and match with your tokens. Just for a basic idea you can think like this

step 1. create tokens from stirng tokenizer

step 2. match the right most string and match with tokens

step 3. match one by one string from right and match with tokens and calculate

for example ONE THOUSAND FIVE HUNDRED FIFTY THREE

let sum =0

first string = THree so sum +=3

second string = FiFTY so sum += 50;

third string = Hundred so you need to multiply fourth string with 100 and add so sum += f*100.. and so on

This is just a basic idea. So you can implement it perfectly after proper planning

Upvotes: 2

Semih Yagcioglu
Semih Yagcioglu

Reputation: 4101

I am not sure if there is a library for that but here is a good example.

Upvotes: 5

Related Questions