0

So this is the continuation of my question from this thread: here

    String[] words = {"cowboy", "animal", "monster"};
    String[] meanings = {"meaning1", "meaning2", "meaning3"};

    boolean check = false;

    for (int i = 0; i < words.length; i++) {
    if (s.toLowerCase().contains(words[i].toLowerCase())) {
        check = true;
    } else {

    }
    }
   if (check) {
   System.out.println("Yes");
   } else {
   System.out.println("No");
   }

But I can't seem to figure out how and where to put the specific meaning of each word that has been typed by the user in the edittext. Any help would be gladly appreciated. Thanks.

Community
  • 1
  • 1
Dunkey
  • 1,900
  • 11
  • 42
  • 72
  • See whether the following thread helps: http://stackoverflow.com/questions/7493287/android-how-do-i-get-string-from-resources-using-its-name – Daniel Aug 25 '13 at 18:59

7 Answers7

1

what about something like that?

int index;
...
if (s.toLowerCase().contains(words[i].toLowerCase())) {
    check = true;
    index = i;
} else {
....
if (check) {
   System.out.println(meanings[index]);
} else {
...
Tuxes3
  • 439
  • 2
  • 19
1

If the words and their meanings have the same relative positions in the two arrays then re-use the index i to print the corresponding meaning as well. But, the i would now have to be defined outside the loop so that it has a bigger scope now and is available inside the if check.

int i = 0
for (; i < words.length; i++) {
  if (s.toLowerCase().contains(words[i].toLowerCase())) {
    check = true;
    break;
  }
}
if (check) {
   System.out.println("Meaning found: " + meanings[i]);
}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
1

Since words and meanings arrays have the relative indexing, you can-

boolean check = false;

for (int i = 0; i < words.length; i++) {

    if (s.equalsIgnoreCase(words[i])) { // We don't need "toLowerCase"

        System.out.println("Word: " + words[i] + "\tMeaning: " + meanings[i]);
        check  = true;
        break; // Match found; so get out of the loop

    }
}

if(check){
    System.out.println("Found!");
} else {
    System.out.println("Not Found!");
}
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
1
    static String[] words = { "Cowboy", "animal", "monster" };
    static String s = "My animal is a Cowboy";
    static boolean check = false;
    static String[] meanings = {"meaning1", "meaning2", "meaning3"};
    static ArrayList<String> typed_words = new ArrayList<String>();
    static ArrayList<String> typed_meanings = new ArrayList<String>();


        for (int i = 0; i < words.length; i++) {
            if (s.toLowerCase().contains(words[i].toLowerCase())) {

                typed_words.add(words[i]);
                typed_meanings.add(meanings[i]);
                check = true;

            } else {

            }

        }

        if (check) {
            System.out.println("Yes");
            for(int i=0;i<typed_words.size();i++){
                System.out.println("Word: "+typed_words.get(i)+" Meaning: "+typed_meanings.get(i));
            }

        } else {
            System.out.println("No");
        }
fida1989
  • 3,234
  • 1
  • 27
  • 30
1

Why not use a java.util.HashMapin which every word maps to a meaning?

String s = "";
HashMap<String,String> meaningMap = new HashMap<String,String>();
//just for the example
meaningMap.put("cowboy", "meaning1");
...
if (s.toLowerCase().contains(words[i].toLowerCase())) {
    ouput = meaningMap.get(s);
}
...
System.out.println(s);
1

Another idea would be to use HashMap to store your data.

HashMap hm = new HashMap();
hm.put("cowboy", "meaning1");
hm.put("animal", "meaning2");

And then use

String s = yourEdittext.getText();
String[] words = s.split("\s+");

for (int i = 0; i < words.length(); i++) {
  words[i] = words[i].replaceAll("[^\w]", "");
}

for (int i=0; i < words.length(); i++) {
  if (hm.containsKey(words[i])) {
    System.out.println("Meaning found: " + hm.get(words[i]));
  }
}

This basically gets the sentece form the edittext, splits it into words then checks if each word is in the HashMap. This is good if your list of words is going to be quite big in comparison to the length of the sentence.

Chris K.
  • 977
  • 8
  • 18
0

i would suggest that you make a Hashmap<String ,ArrayList<String>>

as you have more than one meaning for oneword.

so now just passing the word you need to find the meaning for.

you can.also use treemap if you need to store them in a sorted fashion.

so here's how you can find the meaning for a word .

ArrayList<String> meaning =map.get("cowboy");

for(String string : meaning ) { Log.v("meaning",string); }

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42