Results 1 to 3 of 3
- 09-28-2013, 10:11 PM #1
Member
- Join Date
- Sep 2013
- Posts
- 2
- Rep Power
- 0
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;
}
}
}
}
- 09-28-2013, 11:09 PM #2
Member
- Join Date
- Sep 2013
- Posts
- 10
- Rep Power
- 0
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
- 09-28-2013, 11:37 PM #3
Member
- Join Date
- Sep 2013
- Posts
- 2
- Rep Power
- 0
Re: Trivia Game Using Array Lists HELP!
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?
Similar Threads
-
Space Trivia for Android 0.1
By java software in forum Java SoftwareReplies: 0Last Post: 04-06-2013, 09:59 AM -
Parallel Array Lists?
By Solarsonic in forum New To JavaReplies: 24Last Post: 05-18-2011, 06:18 AM -
array lists in jsp and google api
By gedas in forum New To JavaReplies: 3Last Post: 04-07-2011, 03:18 PM -
Array Lists help!!
By lilika in forum New To JavaReplies: 12Last Post: 01-04-2011, 03:05 PM -
Help with arrays and array lists
By ambernicole88 in forum New To JavaReplies: 3Last Post: 12-04-2009, 10:47 PM
Bookmarks