This is my first program on Scala. So, I hope I'll get the immunity of stupidity.
The code is a one line modification of a snippet from Programming in Scala.
All I am doing is
- reading a text file
- iterating the lines of the file
- printing the max length of the lines in the file.
That works.
Now, when I try to print each line along with the length of the line with
println (eachLine + ":" + eachLine.length)
it throws an error.
I understand from this link in SO that I am supposed to add a parenthesis somewhere. But where and why?
import scala.io.Source
class Loops {
}
object Loops{
def main (args:Array[String]){
printAllLines("Hello123.txt")
}
def printAllLines(fileName:String){
var maxWidth=0
var lineIterator=Source.fromFile(fileName).getLines;
lineIterator.foreach((eachLine:String) =>
println (eachLine + ":" + eachLine.length)
maxWidth=maxWidth.max(eachLine.length) //Compilation ERROR at this line
)
Console.out.println (maxWidth)
}
//THIS WORKS !!
def printAllLinesFor(fileName:String){
var maxWidth=0
for (eachLine<-Source.fromFile(fileName).getLines.toList){
println (eachLine + ":" +eachLine.length)
maxWidth=maxWidth.max(eachLine.length)
}
println (maxWidth)
}
}
ERROR : value maxWidth is not a member of Unit //possible cause: maybe a semicolon is missing before `value maxWidth'?