0

I am tasked to develop a program that prompts users to create their own questions and answers, which will be stored into the arrayList. After that, whenever the user types the same question, the program will automatically extract the answer.

What I did so far: I manage to store the questions and answers into the arrayList, but I have no idea how to trigger the program to extract exact answer when the user asked the question that he had just created. Here are my codes :

import java.util.ArrayList;
import java.util.Scanner;

public class CreateQns {

    public static void main(String[] args) {
        String reply;
        ArrayList qns = new ArrayList();
        ArrayList ans = new ArrayList();
        System.out.println("Type 0 to end.");

        do {
            Scanner input = new Scanner (System.in);
            System.out.println("<==Enter your question here==>");
            System.out.print("You: ");
            reply = input.nextLine();
            if(!reply.equals("0")) {
                qns.add(reply);
                System.out.println("Enter your answer ==>");
                System.out.print("You: ");
                ans.add(input.nextLine());
            }
            else {
                System.out.println("<==End==>");
            }
        }while(!reply.equals("0"));
    }

}
5
  • 1
    (1) Ask the user to type in the question he/she wants to find, (2) find the index of that question in the questions ArrayList, and (3) take the answer with the same index out of the answers ArrayList. Commented Jul 24, 2017 at 13:18
  • @ElanHamburger relying on the exact index of the answer in another List can be quite unreliable. Commented Jul 24, 2017 at 13:23
  • @Rhayene Correct, but the question specifically states that ArrayList should be used. You are completely correct that a Map would be better; I was just attempting to answer within the parameters of the question. Commented Jul 24, 2017 at 13:26
  • I read the question as requiring the use of the ArrayList. On second look, it's ambiguous as to whether the ArrayList is part of the requirements or part of OP's solution Commented Jul 24, 2017 at 13:28
  • @XxS0ul678 please consider my answer Commented Jul 24, 2017 at 13:32

3 Answers 3

2

You may use a HashMap<String, String> which stores Key/value The user enter a question, check if it is in the map, if yes print the answer, if not ask the answer and store it :

public static void main(String[] args) {
   String reply;
   HashMap<String, String> map = new HashMap<>();
   System.out.println("Type 0 to end.");
   do {
       Scanner input = new Scanner(System.in);
       System.out.println("<==Enter your question here==>");
       System.out.print("You: ");
       reply = input.nextLine();
       if (!reply.equals("0")){

          if (map.containsKey(reply))               // if question has already been stored
               System.out.println(map.get(reply));  // print the answer
          else {

               System.out.println("Enter your answer ==>");
               System.out.print("You: ");
               map.put(reply, input.nextLine());         // add pair question/answer
          }
        }else{
                System.out.println("<==End==>");
        }
   } while (!reply.equals("0"));
}

But to answers directly to what you ask, instead of the map.contains() you should do :

int index;
if ((index = qns.indexOf(reply)) >= 0){
    System.out.println(ans.get(index));
}

But that is less convenient, less powerfull than Map

Sign up to request clarification or add additional context in comments.

1 Comment

Using a Map is still better, but if you are going to just search for it in a List the contains call is redundant. Use indexOf and check that the index is >= 0. This way you only need to search the list once.
1

Please find the code without using the HashMap.

import java.util.ArrayList;
import java.util.Scanner;

public class CreateQns {

    public static void main(String[] args) {
        String reply;
        ArrayList<String> qns = new ArrayList();
        ArrayList<String> ans = new ArrayList();
        System.out.println("Type 0 to end.");

        do {
            Scanner input = new Scanner (System.in);
            System.out.println("<==Enter your question here==>");
            System.out.print("You: ");
            reply = input.nextLine();
            if(!reply.equals("0")) {
                if(qns.contains(reply))
                {
                    System.out.println("your answer is==>"+ans.get(qns.indexOf(reply)));
                }
                else
                {
                    qns.add(reply);
                    System.out.println("Enter your answer ==>");
                    System.out.print("You: ");
                    ans.add(input.nextLine());
                }

            }
            else {
                System.out.println("<==End==>");
            }
        }while(!reply.equals("0"));
    }

}   

Comments

0

You need to use a Map<String, String> to correlate the question you are asking to the response the user typed in for it.

Your code should say: if the questions map contains the question the user just typed in, then print the value associated to the question in the map, otherwise ask the user to type and answer and add the question/answer to the map.

Comments

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.