Created
February 13, 2015 13:49
-
-
Save javimolla/6ea38f4bb524c706bff1 to your computer and use it in GitHub Desktop.
Read quotes and print one randomly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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