Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would approach this problem by identifying what is needed. You have a list of categories (Strings). Within each category, there will be a list of question (String) and answer (String) pairs. From there we already see some "logical" ways to organize the data.</p> <ul> <li>Questions - String</li> <li>Answers - String</li> <li>Question/Answer pairs - Write a class (for now, lets refer to it as QAPair) with two Strings as fields (one for the question, one for the answer)</li> <li>List of Q/A pairs within a category - ArrayList</li> <li>List of Categories, mapped to a list of Q/A pairs - Maybe a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Map.html" rel="nofollow">Map</a> would do the trick. The type would be: Map></li> </ul> <p>From there you would start parsing the file; for the first line, or after a blank line is encountered, you know the String will give a category name. You can call containsKey() to check if the category name already exists; if it does fetch the ArrayList of Q/A Pairs for that category and keep adding to the list, otherwise initialize a new ArrayList and add it to the map for that category.</p> <p>You could then read a pair of lines. For each pair of lines you read initialize a QAPair object, then add it to the ArrayList for the category they belong to.</p> <p>Here's an example of using a Map:</p> <pre><code>Map&lt;String, ArrayList&lt;QAPair&gt;&gt; categories = new HashMap&lt;String, ArrayList&lt;QAPair&gt;&gt;(); if (!categories.containsKey("Math")) { // Check to see if a Math category exists categories.put("Math", new ArrayList&lt;QAPair&gt;()); // If it doesn't, create it } QAPair question1 = new QAPair("2+2", "4"); // get() method returns the ArrayList for the "Math" category // add() method adds the QAPair to the ArrayList for the "Math" category categories.get("Math").add(question1); </code></pre> <p>To get the list of categories from a map and pick one:</p> <pre><code>// Convert to an array of Strings String[] catArray = categories.toArray(new String[0]); // Get the 10th category in the array // Use catArray.length to find how many categories there are total catArray[10]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload