-7

I'm developing an application where user will interact using Voice. Whenever the user says first, second, third etc.. i need to convert string "first" to int 1, string "second" to int 2.

So far what i have found out is that(Java-Android How to convert letters in a string to specified number), converting each letter in a string to respective number and then return some number. But i this is not what i need. I'm really stuck. Any help would be appreciated.

Community
  • 1
  • 1
coders
  • 719
  • 1
  • 11
  • 35
  • 2
    There is no built in API for this. You're going to have to write it completely yourself. How high a number are you going to need? If its relatively small, a simple map will do it. – Gabe Sechan Apr 12 '17 at 06:02
  • Thanks for your reply, i know there is no built in api. I don't how long number might grow. – coders Apr 12 '17 at 06:08
  • Possible duplicate of http://stackoverflow.com/questions/4062022/how-to-convert-words-to-a-number – opatut Apr 12 '17 at 11:22

4 Answers4

4

There is no exact way to do so . But for getting the functionality get done u can use Switch case For example:-

switch(WordNumber) {
   case First:
       System.out.println(1);
       return 1;
      break; // optional

   case Two:
       System.out.println(2);
       return 2;
      break; // optional

   // You can have any number of case statements.
   default : // Optional
      // Statements
}
Amit Gujarathi
  • 1,090
  • 1
  • 12
  • 25
  • Good solution, since OP commented after your answer that there is not specific limit, I would say the answer is still valid ! – AxelH Apr 12 '17 at 06:38
2

You will need to split the value receive, to analyse each step. Since you don't have limited value, you will need to create a map for every numbers ... this is a too much.

So just create something like :

{ "one", "two", "three", ... "nine" }
{ "ten, "twenty", "thirteen", ... "nineteen"}
.. hundreds
.. thousands
.. specific case for eleven, twelves, ...

Then you wil need to analyse the value like "twenty two" by spliting it and search for each corresponding.

twenty = index 1 of tens array => (1 + 1) * 10 = 20
two = 2 => index 1 in unit array => 1 + 1 = 2

20 + 2

22

Off course, this need to be think a bit more, but this will give you a good system to read every number possible.

AxelH
  • 14,325
  • 2
  • 25
  • 55
1

If you define a list with all those string cardinals, then you can use indexOf and get the value you are looking for but carefully consider the null element: either add a null-cardinal element to the list or add 1 to the index value

List<String> cardinal = Arrays.asList("first", "second", "third", .. etc);
System.out.println(cardinal.indexOf("first") + 1);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

I'm guessing you have at least some way of getting the string values?

If the numbers are relatively small (~10), then a hashmap can do it. Just save them manually beforehand and look them up by the String.

HashMap<String, Integer>

If you got a much larger list (~100), you are going to have a method that would divide the incoming int, into sub numbers. So in this case, you would look at ten value and combine it with the post value.

2 = Second

22 - Twenty-second 

52 - Fifty-second
Kia
  • 124
  • 1
  • 1
  • 10