Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From what I can see in the question so far, You would be best off creating your own <code>TriviaQuestion</code> Object which would look something like</p> <pre><code>public class TriviaQuestion { public String question; public String answer; public boolean asked; public String category; TriviaQuestion (String q, String a, String c) { question = q; answer = a; category = c; } } </code></pre> <p>Then you have a few options, but if you have this Object then everything becomes a bit easier. I would create a <code>Map&lt;String,List&lt;TriviaQuestion&gt;&gt;</code> where the key is your category.</p> <p>Then when reading the file, also you should use <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#hasNextLine%28%29" rel="nofollow"><code>infile.hasNextLine()</code></a> and <code>inFile.nextLine()</code></p> <ol> <li>Read a line (first I assume would be the category)</li> <li>Read next two lines (question and answer) <ul> <li>Create new instance `new TriviaQuestion( question, answer, category)'</li> <li>Add this to the Array list</li> <li>Repeat until blank</li> </ul></li> <li>If next line is blank, add list to map and loop back to (1)</li> </ol> <p>Like: (this is assuming well formed file)</p> <pre><code>String line = inFile.nextLine(); //first line String category = line; while(infile.hasNextLine()) { line = inFile.nextLine(); if(line.isEmpty()) //blank line category = inFile.nextLine(); else { String q = line; String a = inFile.nextLine(); //do other stuff } } </code></pre> <p>Then to ask a question get the list for the category, choose a random question then set it to asked so it doesn't come up again</p> <pre><code>ArrayList&lt;TriviaQuestion&gt; questions = yourMap.get("Science"); Integer aRandomNumber = 23 //(create a random Number using list size) TriviaQuestion questionToAsk = questions.get(aRandomNumber) System.out.println(questionToAsk.question) questionToAsk.asked = true </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. VO
      singulars
      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