0

I want to create a program that randomly prints out a line from a .txt file. This is where I'm currently at, and the only other similar questions I could find were in other languages. For example, Python with the random.choice() operation, which I found in this question: How to choose a random line from a text file

Thank you all for your time!

fun main() {
    val file = "text.txt"
    println(file.random("text.txt")) //This code doesn't work, I'm just illustrating what I was looking to do.
}

Edits I felt were necessary: The libraries I'm Importing.

import java.io.FileReader
import kotlin.system.exitProcess
import java.io.FileWriter
import kotlin.random.Random

More things I've learned:

There is a function RandomAccessFile which is used for the exact purpose of what I want to do, however, I am not finding any good sources for how to use it within Kotlin.

Edits for Comments: I can read from the file and when I do so all of the lines get printed in order.

1: I know how to generate random numbers, however, I don't know how to add that to the .txt file.

2: I attempted to use the following code to be able to add a number corresponding to the line it was on, however, this code gave me an error for running where the i variable was not understood as an existing number.

ERRORED CODE BELOW AND IN PASTEBIN FOR TIDYNESS. https://pastebin.com/FxFWjv37

fun main() {
    var i = 1
    println("Please input a value, type DONE when done, READ to print.")
    val loop = 0
    while (loop < 1) {
        var response = readLine()
        if (response == "DONE") {
            exitProcess(0)
        }else if (response== "READ") {
            RandomRead()
        } else {
                WriteToFile(i + response)
            i+1
        }
    }
}

Error:(23, 31) Kotlin: None of the following functions can be called with the arguments supplied:

public final operator fun plus(other: Byte): Int defined in kotlin.Int

public final operator fun plus(other: Double): Double defined in kotlin.Int

public final operator fun plus(other: Float): Float defined in kotlin.Int

public final operator fun plus(other: Int): Int defined in kotlin.Int

public final operator fun plus(other: Long): Long defined in kotlin.Int

public final operator fun plus(other: Short): Int defined in kotlin.Int

I also attempted:

fun main() {
    println("Please input a value, type DONE when done, READ to print.")
    val loop = 0
    while (loop < 1) {
        var response = readLine()
        if (response == "DONE") {
            exitProcess(0)
        }else if (response== "READ") {
            RandomRead()
        } else {
                WriteToFile(response)
        }
    }
}
fun WriteToFile(str: String?) {
    var i = 0
    try {
        var fo=FileWriter("test.txt")
        fo.write(i + " " + str + "\n")
        fo.close()
        i+1
    }catch (ex:Exception){
        println(ex.message)
    }
}

Error:(37, 20) Kotlin: None of the following functions can be called with the arguments supplied:

public final operator fun plus(other: Byte): Int defined in kotlin.Int

public final operator fun plus(other: Double): Double defined in kotlin.Int

public final operator fun plus(other: Float): Float defined in kotlin.Int

public final operator fun plus(other: Int): Int defined in kotlin.Int

public final operator fun plus(other: Long): Long defined in kotlin.Int

public final operator fun plus(other: Short): Int defined in kotlin.Int

  • 1
    Can you read the text file line by line? Do you know how to generate random integers? – Peter O. Jun 27 '20 at 23:01
  • 1
    Look into the `readLines` extension function for a `File` object, that gives you a `List`. Then you only need to pick a random line. – Carson Graham Jun 28 '20 at 05:09

1 Answers1

2

As mentioned in the comments, use readLines() in combination with random():

File("file.txt").readLines().random()

But, as the documentation of readLines() says:

Do not use this function for huge files.

Abby
  • 1,610
  • 3
  • 19
  • 35