Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to fill an ArrayList from a file and format the output.
    text
    copied!<p>I am working on this project here for school and I really am stuck at this point. Basically I have to take a text file. Then I have to fill an arraylist(s) with the information inside it. After that I need to format it correctly so that I can use it for Category, question, and answer. I have no idea how to do that, so below is what the instructor asks us if that helps clear it up any further. </p> <blockquote> <p>"In this assignment you will write an application that simulates a trivia game. You will be given a text file that contains a set of questions and answers in six different categories. Your program will select a random question from each category to be answered by the player. The score will be displayed at the end of each run.</p> <p>Input data format: The input file contains questions and answers in different categories. For each category, the first line indicates the name of the category. This line will be followed by a number of pairs of lines. The first line of the pair is the question, and the second line is its corresponding answer. A blank line separates the categories. Technical requirements:</p> <p> Create a class called TriviaQuestion that represents a typical question object.</p> <p> After reading the data, store the information inside ArrayList(s).</p> <p> Be user friendly in your input/output. Ignore cases. Accept partially correct answers if possible."</p> </blockquote> <p>Right now as my code will show I am stuck on how to add the info to the ArrayList in the format that he has specified. I don't want anyone to do my work for me. I just need nudged in the proper direction. I dont know if I need many arraylists for each category and if I do, do I need them to be in seperate classes? This is only my second semester so I'm still pretty perplexed by these things. There are other jave trivia games online and on this forum but I can't find any that work in this fashion. Anyway, here is the code and the text file afterwards....</p> <pre><code>import java.io.File; import java.util.ArrayList; public class TriviaGame { /** * I want to make several array lists and then use a random question for each category. Then compare the user * answer to the real answer to see if they won. Keep a count of the correct answers. Ignore case. */ //instance fields private String player; // The player private int points; // Player's number of points private String currentAnswer; // Current typed answer private File gameFile = new File ("trivia.txt"); //constructors public TriviaGame(String playerName) { playerName = player; points = 0; } public TriviaGame(String player, int points, String currentAnswer, File gameFile, ArrayList&lt;String&gt; triviaQuestion) { super(); this.player = player; this.points = points; this.currentAnswer = currentAnswer; this.gameFile = gameFile; } //Getters and Setters /** * @return the player */ public String getPlayer() { return player; } /** * @param player the player to set */ public void setPlayer(String player) { this.player = player; } /** * @return the points */ public int getPoints() { return points; } /** * @param points the points to set */ public void setPoints(int points) { this.points = points; } /** * @return the currentAnswer */ public String getCurrentAnswer() { return currentAnswer; } /** * @param currentAnswer the currentAnswer to set */ public void setCurrentAnswer(String currentAnswer) { this.currentAnswer = currentAnswer; } /** * @return the gameFile */ public File getGameFile() { return gameFile; } /** * @param gameFile the gameFile to set */ public void setGameFile(File gameFile) { this.gameFile = gameFile; } //To String Method /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "TriviaGame [player=" + player + ", points=" + points + ", currentAnswer=" + currentAnswer + ", gameFile=" + gameFile + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; } } </code></pre> <p>followed by the tester.....</p> <pre><code>import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; import javax.swing.JOptionPane; public class TriviaGamePlayer { /** * @param args */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File gameFile = new File ("trivia.txt"); ArrayList&lt;String&gt; triviaQuestion = new ArrayList&lt;String&gt;(); Scanner infile = new Scanner(gameFile); String line; while(infile.hasNext()){ line = infile.nextLine(); triviaQuestion = line.split(); } } } </code></pre> <p>finally the some of the text file that accompanied it.....</p> <pre><code>Arts &amp; Literature What are bongo drums traditionally held between for playing? The knees The Hugo Awards are given for the best literature in which genre? Science fiction What four-letter girl's name gave Jane Austen the title of a comic novel? Emma Andy Warhol was born in what US city? Pittsburgh Who wrote the novel "The Chocolate War"? Robert Cormier What surname for John in "The Importance of Being Earnest" did Oscar Wilde take from the seaside town he vacationed at? Worthing Geography What is the modern day equivalent of Dacia? Romania What is the capital of Ontario? Toronto Which island boasts of being Napoleon's birthplace? Corsica What nationality is a Breton? French A person from North Carolina is properly known as a what? North Carolinian In which country would you find St. Basil's cathedral? Russia </code></pre>
 

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