Note that there are some explanatory texts on larger screens.

plurals
  1. POUse while loops to read file
    primarykey
    data
    text
    <p>I'm working on a project for school and I'm having some trouble here. I am to go through a file for a trivia game. The format of the file is as follows: The first line of the file will be the category of the questions, the following lines will be pairs of questions and answers. Until it hits a blank line, that indicates that the next line after the blank line starts a new category and it goes on. I am supposed to make an ArrayList that will have 6 indexes. One for each category, then in each of the indexes i should have groups of questions and answers that I will be able to utilize for comparison. I guess essentially an array inside of an arraylist. I hope that what im trying to accomplish makes sense. Its very confusing to me. But here is the code of what Im trying to work on and with so many parts im getting very messed up.</p> <pre><code>import java.util.ArrayList; public class TriviaQuestion { private String player; private String category; private String question; private String answer; private int score = 0; /** * */ public TriviaQuestion() { player = "unknown"; category = "unknown"; question = "unknown"; answer = "unknown"; score = 0; } public TriviaQuestion(String category, String question, String answer){ } public TriviaQuestion(String question, String answer){ } public TriviaQuestion(String player, String category, String question, String answer, int score) { super(); this.player = player; this.category = category; this.question = question; this.answer = answer; this.score = score; } /** * @return the player */ public String getPlayer() { return player; } /** * @param player the player to set */ public void setPlayer(String player) { this.player = player; } /** * @return the category */ public String getCategory() { return category; } /** * @param category the category to set */ public void setCategory(String category) { this.category = category; } /** * @return the question */ public String getQuestion() { return question; } /** * @param question the question to set */ public void setQuestion(String question) { this.question = question; } /** * @return the answer */ public String getAnswer() { return answer; } /** * @param answer the answer to set */ public void setAnswer(String answer) { this.answer = answer; } /** * @return the score */ public int getScore() { return score; } /** * @param score the score to set */ public void setScore(int score) { this.score = score; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "TriviaQuestion [category=" + category + ", question=" + question + ", answer=" + answer + "]"; } } </code></pre> <p>Then the tester, I tried to take some notes about it at the bottom in class, but they don't make a lot of sense to me at this point. </p> <pre><code>import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class TriviaQuestion2 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File gameFile = new File("trivia.txt"); Scanner inFile = new Scanner(gameFile); ArrayList&lt;TriviaQuestion&gt; question = new ArrayList&lt;TriviaQuestion&gt; (); while (inFile.hasNextLine()){ boolean cat = inFile.hasNextLine(); boolean more = true; while (inFile.hasNextLine() &amp;&amp; more); String answer; TriviaQuestion temp = new TriviaQuestion(question, answer); question[0] = new ArrayList&lt;TriviaQuestion&gt;(); new TriviaQuestion(q,a); question[0].add(temp); } } } inFile.close(); //use a while loop inside a while loop, and first do category, then question/answer //there are six categories, use array of trivia questions ArrayList &lt;triviaquestions&gt;[] //question = new ArrayList&lt;triviaQuesitons&gt;[6] //question[0] = new ArrayList&lt;triviaQuesitons&gt;(); /** * while in question[0] and so on read the quesiton and answer * temp = new tq(question, answer) * question [0].add(temp) * question[0].get(i) * */ System.out.println(question); } // TODO Auto-generated method stub } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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