Trivia Game Using Array Lists HELP!
I'm trying to use a loop to read lines from a file and put them into an array list. Every odd line from the file is a question and every even line is an answer for a trivia game. what I want to do with every 2 lines is put them into a question object example (public MashackTrivia(String question, String answer)). Then I need to put each question object into my array. Here is an example of my code so far, let me know if I need to make my question clearer.
public class TriviaTester {
public static void main(String[] args) throws IOException {
ArrayList<MashackTrivia> cat1 = new ArrayList<MashackTrivia>();
Scanner reader = new Scanner(new File("trivia.txt"));
while((reader.hasNext())){
tempquestion = reader.nextLine();
if(tempquestion.isEmpty()){
break;
}
}
}
}
Re: Trivia Game Using Array Lists HELP!
Ok so you know that every odd line is a question and you got your loop going. What exactly is your question? What is giving you trouble?
A few questions for you that might help you out
How do you create an object? (Take a look at Scanner you are creating an object there)
How do you add an object to a list?
Why is there a break in the loop?
What happens if the file happens to be like so with some white space:
1. Question
2. Answer
3.
4.
5. Question
6. Answer
Re: Trivia Game Using Array Lists HELP!
Quote:
Originally Posted by
Ubiquitous
Ok so you know that every odd line is a question and you got your loop going. What exactly is your question? What is giving you trouble?
A few questions for you that might help you out
How do you create an object? (Take a look at Scanner you are creating an object there)
How do you add an object to a list?
Why is there a break in the loop?
What happens if the file happens to be like so with some white space:
1. Question
2. Answer
3.
4.
5. Question
6. Answer
There are white spaces in the file. The file contains 5 categories of questions/answers each separated by a white space. I want to put each category into a separate array list. I'm not sure why I have the break in the loop though. The whole reading process is pretty confusing to me. I did do some reading on it but i'm still not sure how to pull specific lines out of the file and add it to an object. I know .nextLine() will take the next line but what exactly should i do from there?