Java reading past saved variables

I am taking questions from user input which is being scanned for information and i am saving the information and the original question.

What is the best way to save the question+information so that it can be recalled so that i am able to read X questions before the question just asked and get the information/variables on it?

An example:

[User]"what is 1+1?"
[Reply]"two"
save information: "what is 1+1","two"

[User]"how is the dog?"
[Reply]"fine"
save information:"how is the dog?",subject = "dog", condition = "fine"

getQuestion(1) -> "what is 1+1?"
getInformation(current) -> subject = "dog" condition = "fine"

Also what would be better to call the information for each question using, an int (question X), string (question) or other?

Code so far:

Map<String,String> variable = new HashMap<>();
    public QuestionInfo(String name,String variable) {
        this.variable.put(name, variable);
    }
    public String get(String name){
    if(variable.containsKey(name)) {
        return variable.get(name);
    } else {
        return "";
    }
    }

I though about using:

  Map<String,QuestionInfo> Questions = new HashMap<>();

first but then if i wanted to call i cant use an int so instead:

Map<Integer,QuestionInfo> Questions = new HashMap<>();

then i can add the question as the first piece of information.

Answers


Since you want to preserve the order of responses, you probably want to use a List.

Then you just need to decide what kind of list to make. Since you have two items that are closely related - the subject (a String) and condition - I would probably create a custom class:

import java.util.Map;
public class QuestionInfo {

    private String question;
    private Map<String, String> variables;

    public QuestionInfo(String question, Map<String, String> variables) {
        this.question = question;
        this.variables = variables;
    }

    public String question() {
        return question;
    }

    public Map<String, String> vars() {
        return variables;
    }
}

Then you would create your list to store these like this, for example:

List<QuestionInfo> questionList = new ArrayList<QuestionInfo>();

There are other ways to store these strings (for example, you could concatenate them with a delimiter, or use a String[2]).. but this will probably be the easiest to read and understand later, in my opinion.

Here's an approximation of what the user would enter, and how you would use the questionList + QuestionInfo to store the data:

[User] "how is the dog?" [Reply] "fine"

Map<String, String> varMap = new HashMap<String, String>();
varMap.put("subject", "dog");
varMap.put("condition", "fine");
questionList.add(new QuestionInfo("how is the dog?", varMap));

Now you can access this info by using:

questionList.get(0).question() // returns "how is the dog?"

or

questionList.get(0).vars().get("condition") // returns "fine"

Note that this is glossing over the code to actually gather the user input.

There are other possibilities. You could pass in a String... to the QuestionInfo constructor and parse the variables/create the map there, for example. You also may want to store the entire response as a String separate from the variables. You may also want to create an enum for the keys, if you have a consistent set of keys you'll be populating.


I think I would just create a "Question" class that would store the actual question and the information you want (subject, condition, etc.) and just store them in an array.

Some code adding your comment about having multiple answers (it's missing the setters, but I guess you get the idea):

public class Question {
    private String subject;
    private List<String> responses;

    public String getSubject(){
        return subject;
    }
    public String getResponse(int _index){
        if (_index <= response.size())
        {
            return response.get(_index);
        }
        return "";
    }
}

Need Your Help

phonegap 1.7.0 plugins android not working

javascript android cordova phonegap-plugins applicationcraft

I'm programming an android app using Applicationcraft, but then I need to create my own plugin for a certain requirement. So I downloaded my app from applicationcraft, then compile it manual using

How do I trigger a textbox with a button

java button textbox

I have the following code. How would I make a textbox displaying the text on each button come up when that button is pushed? I'm taking a beginning java class online that gives little to no instruc...