Skip to content

Instantly share code, notes, and snippets.

@javimolla
Created February 13, 2015 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javimolla/6ea38f4bb524c706bff1 to your computer and use it in GitHub Desktop.
Save javimolla/6ea38f4bb524c706bff1 to your computer and use it in GitHub Desktop.
Read quotes and print one randomly
import scala.collection.mutable.ArrayBuffer
import util.Random.nextInt
object test extends App {
val source = scala.io.Source.fromFile("/path/to/quotes.txt")
val quotes = new ArrayBuffer[String]()
var current = ""
for (line <- source.getLines) {
if (line != "") {
if (current.length() != 0) current += "\n"
current += line
} else {
quotes += current
current = ""
}
}
println(quotes(nextInt(quotes.length)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment