Results 1 to 3 of 3
  1. #1
    Sheep is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Question 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;
    }
    }

    }

    }

  2. #2
    Ubiquitous is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default 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

  3. #3
    Sheep is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Trivia Game Using Array Lists HELP!

    Quote Originally Posted by Ubiquitous View Post
    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?

Similar Threads

  1. Space Trivia for Android 0.1
    By java software in forum Java Software
    Replies: 0
    Last Post: 04-06-2013, 08:59 AM
  2. Parallel Array Lists?
    By Solarsonic in forum New To Java
    Replies: 24
    Last Post: 05-18-2011, 05:18 AM
  3. array lists in jsp and google api
    By gedas in forum New To Java
    Replies: 3
    Last Post: 04-07-2011, 02:18 PM
  4. Array Lists help!!
    By lilika in forum New To Java
    Replies: 12
    Last Post: 01-04-2011, 02:05 PM
  5. Help with arrays and array lists
    By ambernicole88 in forum New To Java
    Replies: 3
    Last Post: 12-04-2009, 09:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •