【发布时间】:2016-05-15 17:24:16
【问题描述】:
我正在开发一个应用程序,我必须将整数值分配给不同的字符串。例如我要分配:
John = 2
Good = 3
Person= 7
现在这些 John、Good 和 person 是字符串,而 2,3 和 7 是 int 值。但是我对如何实现它感到很困惑。我读了很多关于如何将 int 转换为 string 和 string 到 int 的内容,但这是不同的情况。
我为用户提供了在 editText 中输入文本的选项,例如,如果用户输入“你好 John,你是个好人”,那么这一行输出将为 12,因为 John、Good 和 person 这三个词都在输入文本。你能告诉我如何实现吗? 我被困在这里是我的小代码:
String s = "John";
int s_value= 2;
现在我想将这个 2 分配给 John,这样每当用户提供输入并且它包含 John 时,就会为 John 显示值 2。请帮助,因为我只是一个初级程序员
这是我的代码(已编辑)
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 + " ");
}
正如你所见,我应用了你的代码,然后我想拆分我的主字符串,以便该字符串的每个单词都与 Map 中的字符串进行比较。现在我很困惑在 for 循环中写什么('stopwords' 会被什么东西代替?)
【问题讨论】:
-
最佳实践是使用枚举(枚举数据类型)
标签: android string integer assign