-4

I am developing a Speech calculator in Android in Java.

How do I read every word in a textview and put it in an if else condition.
So if the words are "five plus two", each word will go into the condition if "five" then textview2 = 5 then read the next word: if "plus" then textview2 = 5 + ... (then I will do the calculations in the end.)

Never studied Mobile Applications, also I don't even know the logic of Java.

Filnor
  • 1,290
  • 2
  • 23
  • 28
  • 3
    good time to start studying JAVA and android :) – gagan singh Jun 28 '18 at 10:39
  • What you are asking involves the very basics of programming. Echoing gagan sing, I suggest it would be useful for you to study these basics before staring any ambitious project. – Teemu Leisti Jun 28 '18 at 10:41
  • I have never worked with Android but I suggest creating a dictionary using a `Map` for the translations of text to numbers and operators. That will be some tedious work but easy to make and understand. https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html – Jack Flamp Jun 28 '18 at 10:45

1 Answers1

0

Well you've got a couple of steps to go through there.

I'm going to stay as simple as I can since you're starting out.

  1. Split the text up into individual words:

You can use built in string methods to achieve this: How to split a string into an array of strings

  1. Use a loop to cycle through the array
  2. Use if blocks to check the text
  3. Compare the text using the String equals method. Would recommend using the equalsIgnoreCase if you don't want it to be case sensitive: String comparison
  4. Then inside the if block assign your variable to the appropriate value

So something like this:

String [] stringArray = input.split("\\s+");

for (each string in the array) {
    if (thisString.equalsIgnoreCase("otherString") {
        assign value;
    }
    else if ....

and so on

If you were feeling confident you can refine this as you go on to be more efficient.

Happy coding :-)