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
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'?
A basic way we can handle exceptions in Scala is the try/catch/finally construct, really similar to the Java one. In the following example, to make testing easier, we'll return a different negative error code for each exception caught: def tryCatch(a: Int, b: Int): Int = { try { return Calculator.
Like Java, Scala has a try/catch/finally construct to let you catch and manage exceptions. The main difference is that for consistency, Scala uses the same syntax that match expressions use: case statements to match the different possible exceptions that can occur.
Throwable is just an alias for java. lang. Throwable . So in Scala, a catch clause that handles Throwable will catch all exceptions (and errors) thrown by the enclosed code, just like in Java.
The Try type represents a computation that may either result in an exception, or return a successfully computed value. It's similar to, but semantically different from the scala. util. Either type. Instances of Try[T] , are either an instance of scala.
Change
lineIterator.foreach((eachLine:String) =>
println (eachLine + ":" + eachLine.length)
maxWidth=maxWidth.max(eachLine.length) //Compilation ERROR at this line
)
to
lineIterator.foreach{ (eachLine:String) =>
println (eachLine + ":" + eachLine.length)
maxWidth=maxWidth.max(eachLine.length) //Compilation ERROR at this line
}
should fix this problem.
Notice the difference between foreach {...}
and foreach (...)
, if your foreach block has multiple lines, you should use {}
.
The foreach
needs braces for a multiline function.
lineIterator.foreach((eachLine:String) => {
println (eachLine + ":" + eachLine.length)
maxWidth=maxWidth.max(eachLine.length) //Compilation ERROR at this line
})
When you add the println line, you extend the block; it had only one line before, but now it has two lines. Therefore, you have to put curly braces around it:
lineIterator.foreach((eachLine:String) => {
println (eachLine + ":" + eachLine.length)
maxWidth = maxWidth.max(eachLine.length)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With