Read the file sequentially – thats the easiest route.
Then randomly shuffle the collection.
Actually, another question. Could you use math.random() to look at the lines of the text file and if, lets say, the line 5 comes up then you remove it from the parameters you set for math.random()? It would pick from 0-9 and after the fifth line is used it would pick from 0-4,6-9
It is possible – but over complicates things. The best way to implement such a feature, is to have a ‘pool’ of numbers. (i.e. an arraylist of Integer objects), then you can use a Random number generator (between 0 and arrayList.size()) to get (and remove it from your arrayList too) one of these Integer objects. Then read that line. This approach needs several objects (Random, Arraylist, Integer, Reader).
At best, its overcomplicated for something so simple. Best thing to do, like I said, read each line and insert it into the arrayList. Then suffle.
Another take on this is, read each line, get the size of the arraylist and insert the new String, randomly within the arraylist. Heres some code:
arrayList.add(getRandomIndex(arrayList.size()),string);
public int getRandomIndex(int size){
return ((int)Math.random()*size)
}
3
solved How can I read a file and randomly pull information from it and add it to an array list? [closed]