Ignore spaces when reading from a file using get lines in Scala -
i trying read inputs file , counts them using map.i want ignore spaces when reading file.
val lines = source.fromfile("file path","utf-8").getlines() val counts = new collection.mutable.hashmap[string, int].withdefaultvalue(0) lines.flatmap(line => line.split(" ")).foreach(word => counts(word) += 1) ((key, value) <- counts) println (key + "-->" + value) when try code following input.
hello hello world goodbye hello world the output is
world-->2 goodbye-->1 hello-->3 -->2 it counts 2 spaces. how can fix ?
lines.flatmap(_.trim.split("\\s+"))
Comments
Post a Comment