0

First off, I've only recently started to use Java, so what might seem obvious to others may not appear so to me.

I'm trying to implement a Quiz application and one of the stipulations is that I read the questions from an external file and store the questions in a MAP structure.

I've looked into this and it appears that a MAP stores a key and a value...

Am I right in thinking that I can store an idetifier as the key and then the rest of the information as the value, even though the rest of it consists of 4 elements of two differing data types (2 ints, a string and an array of strings)?

If my assumtion is correct, how would I implement that, as any documentation I have found resembles the following:

HashMap<Integer, String> questionMap = new HashMap<Integer, String>(); 

Any assistance or nudges in the right direction are greatly appreciated.

The Question class currently consists of (I've remover the getters and setters to save space on here:

public class Question { public int identifier; public String type; public String question; public String[] options; public int answer; } 

3 Answers 3

2

First create a class to hold your question information, then use it for the values in your Map, e.g.:

HashMap<Integer, Question> questionMap = new HashMap<Integer, Question>(); 
Sign up to request clarification or add additional context in comments.

1 Comment

You haven't defined what Question is.
2

the rest of it consists of 4 elements of two differing data types (2 ints, a string and an array of strings)?

This sounds like an Object that you'd want to write your own class for

public class Data { int id: // optional int a, b; String c; String[] d; } 

Then your Map would be of the type <Integer, Data>, and I would suggest a TreeMap if you want ordered questions

Comments

0

Am I right in thinking that I can store an idetifier as the key and then the rest of the information as the value, even though the rest of it consists of 4 elements of two differing data types (2 ints, a string and an array of strings)?

No, both key and value have to be a of a single data type. What you usually would do in that case is create your own data structure that encapsules your data:

public class Question { private int id; private int score; private String question; // Constructor, Getters & Setters //.... } 

Then you can use that dataType as a Value

Map<Integer, Question> questionMap = new HashMap<Integer, Quesiton>(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.