Last active
August 29, 2015 14:24
-
-
Save SteveBate/ff350dc240b637989d64 to your computer and use it in GitHub Desktop.
quick demo of reading csv data and mapping to a value object before printing out a property on each object
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.io.Source | |
// value object - automatically implements equals and gethashcode | |
case class Line(lineNo: Int, partNo: String, title: String, description: String) | |
object Test { | |
// converts string array into Line value object | |
def toLine(args: Array[String]): Line = { | |
Line(args(0).toInt, args(1), args(2), args(3)) | |
} | |
def main(args: Array[String]): Unit = { | |
// read lines, convert each line to a Line value object, then print out part numbers | |
val file = Source.fromFile("data.csv") | |
file.getLines().map(str => toLine(str.split(", "))).foreach(l => println(l.partNo)) | |
file.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment