0

I have been poring over these links: Randomly select an item from a list printing an array from .txt file java How to shuffle the contents of an array

But they are confusing me as much as helping?

I'm pretty sure I want to use the line

public static void shuffle `(String[] array)` // mix-up the array

but I'm not sure what the lines

(String[] array)

are referencing.

I would like to have one method that reads an array (needs to read it first so it knows what to shuffle right??), shuffles it, and then saves the new text file; or reads an array and pulls a random one from the list.

I'm confused by how to use ArrayList together with string.

How do I tell Java what file to read? How do I tell it exactly how to parse what it's reading. Should I just have it read line by line and pull a random line to show me?

public static void Encounter()

Scanner wildInput = new Scanner(new File("WildPkmn.txt"));

randomGenerator = new Random();

    }
}
See:
}
Claem
  • 51
  • 4

2 Answers2

2

You first need to read your file and it can be done as follows:

List<String> result;
try (Stream<String> stream = Files.lines(Paths.get("WildPkmn.txt"))) {
     result = stream.collect(toList());
} catch (IOException e) { e.printStackTrace(); }

Then I'd recommend changing your method signature to:

public static void shuffle (List<String> source){
      // look into Collections.suffle
      // etc..
}

or you may want to get rid of the method altogether since Collections.suffle is one line of code...

However, assuming you strictly want an array and perform further logic based on that then you can change the first example snippet above to:

String[] result;
try (Stream<String> stream = Files.lines(Paths.get("WildPkmn.txt"))) {
     result = stream.toArray(String[]::new);
} catch (IOException e) { e.printStackTrace(); }

Hopefully, that's a good start for you to proceed with your logic...

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
1

I don't understand why you don't simply take advantage of List and Collection's shuffle method. Play with the following.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class Test {
    public static final String file = "WildPkmn.txt";

    public static void main(String[] args) {
        List<String> list = null;
        try {
            list = readFromFile(file);
        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
        }

        System.out.println(list);
        Collections.shuffle(list);
        System.out.println(list);

    }

    public static List<String> readFromFile(String fileName) throws IOException {
        Stream<String> stream = Files.lines(Paths.get(fileName));
        return stream.collect(Collectors.toList());
    }
}

If the string array exists already,

String[] tokens = ...;
Collections.shuffle(Arrays.asList(tokens));