Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer your questions directly:</p> <p>1) You pretty much had your file reader, but you needn't bother with the <code>Map</code>, a <code>List</code> will suffice.</p> <pre><code>Scanner scanner = new Scanner(new FileReader("C:\\Path\\boards.txt")); List&lt;String&gt; lines = new ArrayList&lt;String&gt;(); while (scanner.hasNextLine()) { String columns = scanner.nextLine(); lines.add(columns); } </code></pre> <p>2) To grab a random line, just pick a random index into the <code>List</code>.</p> <pre><code>Random random = new Random(); int randomLineIndex = random.nextInt(lines.size()); String randomLine = lines.get(randomLineIndex); </code></pre> <p>Now, all that said, if you don't need to load the whole file, don't. If you just need to grab a random line from the file then go right for it and save yourself computation and memory.</p> <p>To do so, you'll first need to known the number of lines in the file. <a href="https://stackoverflow.com/questions/1277880/how-can-i-get-the-count-of-line-in-a-file-in-an-efficient-way">This question</a> gives you:</p> <pre><code>String file = "C:\\Path\\boards.txt"; BufferedReader reader = new BufferedReader(new FileReader(file)); int lineCount = 0; while (reader.readLine() != null) lineCount++; reader.close(); </code></pre> <p>Now you know how many lines there are and can pick one at random:</p> <pre><code>Random random = new Random(); int randomLineIndex = random.nextInt(lineCount); </code></pre> <p>Then you can grab that line and ignore the rest:</p> <pre><code>reader = new BufferedReader(new FileReader(file)); int i = 0; while (++i &lt; randomLineIndex) // Skip all lines before the one we want reader.readLine(); String randomLine = reader.readLine(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload