Reputation: 65
(I am a beginner) I am making a decision maker app that gives responses randomly using a switch and changes the text of a TextView as output. What I want to do:
For example, if user asks "Should I post a question on stackoverflow?
taps OK
the app displays an answer randomly(say, Yes! you should do it.).
And next time when the user asks the same question I want the same response (First time response should be random next time it should be same as given before).I am using EditText for input.
I tried doing this with arrays but it does not work. If it is to be done by arrays please explain or give any other method for doing this...Thanks
Upvotes: 0
Views: 42
Reputation: 120
If I was you I'd use a HashMap.
HashMap<String, String> mymap = new HashMap<String , String>();
Then you can save the question and answer like:
String question = "Should I post a question on stackoverflow?"
String answer = "Yes! you should do it"
mymap.put(question, answer);
And then when you want to retrieve the answer for this question you do it like:
mymap.get(question);
Edit:
The question in your HashMap is a key
To retrieve your answer you have to find it by the key(which is your question ).So instead of saying:
mymap.get(answer);
you say mymap.get(question);
If you want to retrieve your question you probably have to write a method like:
public String getQuestion(String userQuetsion){
if(mymap.keySet().contains(userQuestion))
return userQuestion;
}
Upvotes: 1