1

I am developing an app in which i have to assign integer values to different string of words. For Example I want to assign:

John = 2
Good = 3
Person= 7

Now these John, Good and person are strings while 2,3 and 7 are int values. But I am so confused about how to implement that. I read many things about how to convert int to string and string to int but this is different case.

I am giving option to user to enter a text in editText and if for example User enters "Hello John you are a good person" then this line output will be 12 as all the three words John, Good and person are there in the input text. Can you tell me how to achieve that? I am stuck here is my little code:

String s = "John";
int s_value= 2;

now I want to assign this 2 to John so that whenever user give input and it contains John then the value 2 is shown for John. Please Help as I am just a beginner level programmer

Here is my code (Edited)

String input = "John good person Man";
Map<String, Integer> map = new HashMap<>();
map.put("John", 2);
map.put("Good", 3);
map.put("Person", 7);
//int number = map.get("Good");
String[] words = input.split(" ");
ArrayList<String> wordsList = new ArrayList<String>();

for(String word : words)
{
    wordsList.add(word);
}
for (int ii = 0; ii < wordsList.size(); ii++) {
    // get the item as string
     for (int j = 0; j < stopwords.length; j++) {
         if (wordsList.contains(stopwords[j])) {
             wordsList.remove(stopwords[j]);//remove it
         }
     }
}
for (String str : wordsList) {
    Log.e("msg", str + " ");

}

As u see i applied code of you and then i want to split my main string so that each word of that string compares with the strings that are in the Map<>. Now i am confused what to write in the for loop ( 'stopwords' will be replaced by what thing?)

1
  • Best practice is to use enum(enumerated data type) Commented May 15, 2016 at 17:39

4 Answers 4

3

You can use a Map<String, Integer> to map words to numbers:

Map<String, Integer> map = new HashMap<>();
map.put("John", 2);
map.put("Good", 3);
map.put("Person", 7);

and then query the number given a word:

int number = map.get("John"); // will return 2

UPDATE

The following code iterates over a collection of words and adds up the values that the words match to:

List<String> words = getWords();
int total = 0;
for (String word : words) {
  Integer value = map.get(word);
  if (value != null) {
    total += value;
  }
}
return total;
Sign up to request clarification or add additional context in comments.

7 Comments

Ok sir lemme check this one
can u please check my edited post? i applied ur method just need ur little guidance in my final output
I see you're creating the map, but I don't see you using it. Also I'm not 100% sure what you're trying to achieve.
Sir my string "John good person man" must be compared with these strings that i put in map i.e. John, good, person so what i done is that I split my main string and then i want to compare each word of that string with the strings in the map and then the output will be 12 because "John good person Man" has three words that have value in them i.e. the value of john good and person are respectively 2,3 and 7 as we assigned
Thankyou so much Sir. Finally it works. Thanks again
|
0

I would use a Dictionary for this. You can add a string and an int (or anything else actually) value for that string.

 Dictionary<string, int> d = new Dictionary<string, int>();
 d.Add("John", 2); 
 d.Add("Good", 3);
 d.Add("Person", 7);

3 Comments

Ok sir lemme check and if i want to compare the input text with this assigned values then how can i compare? i.e. John contains 2 as u mentioned and if user input John is good then how can i compare this line with that dictionary line
Dictionary? Are we still talking Java?
You can check if your dictionary contains your input text like this: if(d.ContainsKey(input)) { ///code here}
0

You can use String contains to achieve this. Following is the code:

String input = "John you are a good person";
String s1 = "John";
String s2 = "good";
String s3 = "person";
int totScore =0;
if(input.contains(s1)) {
  totScore=totScore+2;
}
else if (input.contains(s2)) {
  totScore=totScore+3;
}
else if (input.contains(s3)) {
  totScore=totScore+7;
}

System.out.print(totScore);

Comments

0

You can use class like.

class Word{
    String wordName;
    int value;

    public Word(String wordName, int value){
        this.wordName = wordName;
        this.value = value;
    }

    // getter
    public String getWordName(){
        return this.wordName;
    }

    public int getValue(){
        return this.value;
    }

    // setter
    public void setWordName(String wordName){
        this.wordName = wordName;
    }

    public void zetValue(int value){
        this.value = value;
    }

}

You can create an object of the word

Word person = new Word("Person",3);

2 Comments

Ok sir lemme try this one
OK, Sure. let me know if you get any problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.