• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with ArrayList(s) and FileIO  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

> "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:
>
>  Create a class called TriviaQuestion that represents a typical question object.

>  After reading the data, store the information inside ArrayList(s).

>  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....



followed by the tester.....



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

 
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd probably have an ArrayList for each category. Your first job though is to create a TriviaQuestion class, and then see if you can parse the file in to TriviaQuestion objects. Once you've got that going, you can worry about storing them in ArrayLists and categories.
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

You need to create separate class for question,answer,player,category. These classes should store corresponding data :
question class should have reference to answer class, reference to category class and String content variable for question.
answer class should have reference to player class and String content variable to store the actual answer. Plus Answer class may need reference to Question
Player should have String fullname variable for his name and int score variable for player's score.
Category class is the most important as it contains ArrayList<Question> questions to store only questions related to this category. Plus Category class
contains String name variable to store name of category.
This is your domain and should be placed in separate package!

Next you create class QuestionManager that has method like
public void readCategory(String filePath){
BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)));
String categoryName = reader.readLine();
String nextLine;
while ((nextLine = reader.readLine()) != null && !nextLine.isEmpty()) {
// reads question on one iteration
// reads answer on next iteration
}
}
 
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I second what Greg has said. Get the IO between your code and the text file working first. However, I would think about it more (no coding) first beforehand.

In such assignments, it is always best to break the problem down into smaller chunks. Do this on paper first, don't just start writing code.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main complexity would be in reading that file.

Yo should use bufferedreader because it reads by line.
Code should be like this :
String nextLine;
while ((nextLine = reader.readLine()) != null) {
//the first nextLine is category name
category.setName(nextLine);
//NEXT you should reuse current reader in nested while loop
while ((nextLine = reader.readLine()) != null && !nextLine.isEmpty()) {
//here one iteration retrieves question and subsequent one retrieves answer
//The most important is that this while loop terminates when you reach empty line
}
}

Good luck...
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bad news. I am going to disagree with you all.
I think you would do better to get all your file reading done in one place and populating your data structure in another. Suggest you start by simply displaying the lines, rather like this:-Then, you can use those Strings to create other sorts of objects. Call another method to use those Strings.
Why are you using a List for your questions and answers? Are you aware of all the different kinds of data structures already supplied by Java™. There is one which I think is much better for Question-Answer pairs.
Why have you not got a Question class and an Answer class?
The advice to use BufferedReader is very old‑fashioned; you can use a Scanner object to read the lines. Consider whether nested loops are the best control structure. Find out about the weird syntax with ) != null in the while loops. It looks strange, but is actually correct syntax.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Eck wrote:Anyway, here is the code and the text file afterwards....

Steven,

Please DontWriteLongLines. It makes your thread very hard to read, and it's actually bad coding practice.
I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long method calls.

Thanks.

Winston

PS: Just for the record, I'm with Campbell on this one.
 
Steven Eck
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here is what I have so far. This basically seperates all of the parts of the text file.
My trouble now is how to get these to sort in a way that I can ask one question from
each category randomly then be able to compare the answer.

 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

You do repeat yourself which may lead to bad result.

I cannot make out your code very well but I recommend to do this way:
1. Create class Category
2. Inside Category create 2d array String[][] questionAnswer to hold the question associated with answer like this
3. From file you should read Question as String and corresponding answer as string and you should store them in 2d array by
questionAnswer[0]=new String[]{question,answer};
4. In TriviaGamePlayer create List<Category> to store your categories.

To take random question-answer pair from questionAnswer of Category class you should call method Math.random() * questionAnswer.length which will give you index which you can then use to take question-answer pair like this:
String question=[randomIndex][0];
String answer=[randomIndex][1];

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Eck wrote:Ok, here is what I have so far. This basically seperates all of the parts of the text file.

Actually, it doesn't. If you really want to separate out the logic, how about something like this:and now in your main() method you simply write:and you now have a fully functional List of the lines in your file that you can do anything you like with.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote: . . .
2. Inside Category create 2d array String[][] questionAnswer to hold the question associated with answer like this
. . .
Disagree. Apart from the fact that there is no such thing as a 2D array, that is non‑object oriented design. You should have a Question or Answer class, or some combination. Using two arrays is also error‑prone.

I agree the use of a random index will allow you to find a suitable question. You may do well to remove the question from the List after you have asked it.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may do well to remove the question from the List after you have asked it.

This won't allow to pick up random question.
OP is at school so I think two-dimensional array is just what to deal with.

On the other hand you can really create Question and Answer classes. Question holds reference to Answer.
And in Category class you then need a List<Question> questions. Again to pick up a random question from List it is possible to use Math.random
 
Steven Eck
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh boy, I appreciate all the help I am getting here. There seems to be a lot of different ways to accomplish this task.
The chapter we just finished was on arrays and arraylists. The professor does want us to only use arraylists for this assignment.
I just not sure if I need a single huge one to pick through or one for each category,
on top of how to get the correct information into the arraylist(s).
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need ArrayList to store all Categories plus you need ArrayList in each Category to store related Questions.
Read my previous answer.
 
Steven Eck
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that I can understand. But I guess my question would be how do I go about making these
so that the correct information goes into the correct cells?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Eck wrote:Oh boy, I appreciate all the help I am getting here. There seems to be a lot of different ways to accomplish this task.

Welcome to programming.

Ok, that I can understand. But I guess my question would be how do I go about making these
so that the correct information goes into the correct cells?

Well, I agree with Volodymyr that you should create a Category class, but what about simply making it a list of TriviaQuestion's (ie, a List<TriviaQuestion>) with a name?

In fact, you could simply extend ArrayList, just as I did above. A lot of people forget that the ArrayList class is not final, so you can extend it any way you like to create your own custom Lists.

Then your entire quiz becomes a List<Category>.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!