samedi 1 août 2015

Remove arraylist string stopping random function

Thanks to the stackoverflow community, my problem was fixed. However, I now have a new problem. I have some code that reads a random line from a file, displays it and now removes the random line from an arraylist so that it won't be displayed again. But now, the lines being shown aren't random. I have a file with 40 lines in it and now it's only showing the last 20 lines, in order. Here's the updated code:

try {
    Random random = new Random();
    int randomInt = random.nextInt(50);
    FileReader fr = new FileReader(file);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line = reader.readLine();
    if (line.contains("!")) {
        List<String> lines = new ArrayList<>();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
            Random r = new Random();
            String rdmln = lines.get(r.nextInt(lines.size()));
            line1.setText("Line" + rdmln);
            lines.remove(rdmln);

The remainder of the code repeats the process from 'lines.add(line)' to 'lines.remove(rdmln)' but instead, it displays 'rdmln2' on the label 'line2', then removes 'rdmln2' and so on. Can anyone see what might be causing this problem? Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire