How to fill an ArrayList from a file while formatting the information at
the same time? [on hold]
I am working on this project here for school and I really am stuck at this
point. Basically I have to take this trivia.txt file that he supplied and
fill an ArrayList with it, that i have somewhat of a clue how to do. But
then I have to format the output so that my program will recognize the
different categories and be able to output them correctly. Here is what he
has written,
"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.
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:
„h Create a class called TriviaQuestion that represents a typical question
object.
„h After reading the data, store the information inside ArrayList(s).
„h Be user friendly in your input/output. Ignore cases. Accept partially
correct answers if possible."
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....
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<String> 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() + "]";
}
}
followed by the tester.....
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<String> triviaQuestion = new ArrayList<String>();
Scanner infile = new Scanner(gameFile);
String line;
while(infile.hasNext()){
line = infile.nextLine();
triviaQuestion = line.split();
}
}
}
finally the text file that accompanied it..... note that after the
category it goes to the next line for the question, then the next line for
the answer, the blank line goes in between the categories. It doesnt seem
to format that way on here when I pasted it.
Arts & 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
Entertainment
How old was Marilyn Monroe when she died?
36
Who created the TV series "Twin Peaks"?
David Lynch and Mark Frost
Who was Charlie's roommate in the 1989 movie "Dead Poets Society"?
Richard
What country singer recorded a popular album at Folsom Prison?
Johnny Cash
In a 2003 movie, Diane Lane plays a writer who buys a villa in what region
of Italy?
Tuscany
What country singer recorded a popular album at Folsom Prison?
Johnny Cash
History
What was John F. Kennedy called by his friends?
Jack
In which country do parts of Hadrian's Wall still remain?
Great Britain
How many dogs survived the sinking of the Titanic?
Two
What were the first words Edison spoke into the phonograph he had just
invented?
Mary had a little lamb
When did construction of the Berlin Wall begin?
1961
What did Benjamin Franklin do on April 17, 1790?
He died
Science & Nature
What is February's birthstone?
Amethyst
What occurs when the Sun and the Moon are in conjunction as viewed from
Earth?
A solar eclipse
What is February's birthstone?
Amethyst
What potentially fatal disease is also known as "rabbit fever"?
Tularemia
How many eyelids does a cat have on each eye?
Three
What kind of poisoning is known as plumbism?
Lead poisoning
Hyperopia is the scientific name for what eye condition?
Farsightedness
Miscellaneous
"Lorgnette" is the proper term for a pair of glasses with what?
A handle
What is a Dubbel?
A Belgian beer style
For what game were two jokers originally included in a standard deck of
playing cards?
Euchre
As what type of beans are chickpeas also known as?
Garbanzo
What do you get when you mix scotch, sweet vermouth and angostura bitters?
A Rob Roy
What cut of steak got its name from the New York City coach stop where it
was first served?
Porterhouse
No comments:
Post a Comment