-1

New to File I/O and java in general. Doing a project that i have to read in text from a txt file assign them to arrays. This is what i have right now, (all this does is read the text from the file and output it to console) just wondering how you assign these to arrays (later implementing in a trivia type game with a gui)?

package pokemontrivia;
import java.io.*;
import java.util.*;

public class Pokemontrivia {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        InputStreamReader reader = new InputStreamReader(Pokemontrivia.class.getClassLoader().getResourceAsStream(".\\pokemontrivia\\pokemontriviaquestions.txt"));
        BufferedReader in = new BufferedReader(reader);
        String line;
    while((line = in.readLine()) != null)
    {
    System.out.println(line);
    }

    }
}

This is what is on my txt file

What shiny Pokemon can you catch in 2nd Generation Pokemon games?

What 2 Pokemon do you need in your party to catch the Regi's? What Pokemon game is from the 3rd Generation?

How many Pokemon species are there?

What Pokemon can you trade for an Onix in 2nd Generation Pokemon games?

Which haircut brother gives you the most friendship?

How many evolutions does Eevee have?

What is the last evolution of Bulbasaur?

Which Pokemon was the first Pokemon to let you play as a girl?

What are the 3 starters for Generation 4?

user3184504
  • 11
  • 1
  • 1
  • 2
  • 1
    You might want to use Vectors rather than arrays, if you don't know in advance how many items you'll be reading in. Outside of that, I'm not sure what you're looking for beyond 'create an array that's long enough, maintain a counter of the number of lines read, assign each line to the array element referenced by the counter's current value; if the array isn't long enough after all copy its contents into a new longer array and continue with that' – keshlam Jan 15 '14 at 00:44
  • http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Samy Dindane Jan 15 '14 at 01:01

2 Answers2

0

Like what was already mentioned, if you don't know how many lines of text you will want to use either a Vector or an ArrayList (I prefer ArrayList personally). If you really need this as an array you can always use ArrayList's toArray() method.

As for adding each question to the ArrayList, you can use the add() method to add each question as a new entry (assuming that each line is a question by itself).

So in this case, I would do something like this:

List<String> questions = new ArrayList<String>();
while((line = in.readLine()) != null)
{
    questions.add(line);
}

Then you can do whatever you want with the list of questions you have. If you want to learn more about the uses of ArrayList I would check this out.

Edit: I realize now that it may not be clear what the <String> is. This is using Generics to help ensure that we know the type of each member of the ArrayList. For more information on Generics, you can check out Java's lesson on Generics.

endorphins
  • 586
  • 1
  • 4
  • 21
0

The simplest way is by using Apache's FileUtils.readLines() from the commons-io library:

List<String> lines = FileUtils.readLines(file);
Bohemian
  • 412,405
  • 93
  • 575
  • 722