Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala first program - error

Tags:

scala

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

  1. reading a text file
  2. iterating the lines of the file
  3. 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'?

like image 882
Arun Manivannan Avatar asked Dec 13 '12 07:12

Arun Manivannan


People also ask

How to handle error in Scala?

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.

How do you catch errors in Scala?

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.

What is throwable Scala?

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.

What is try in Scala?

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.


Video Answer


3 Answers

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 {}.

like image 184
Brian Hsu Avatar answered Oct 20 '22 21:10

Brian Hsu


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
    })
like image 42
Brian Avatar answered Oct 20 '22 21:10

Brian


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) 
}
like image 45
Frank Schmitt Avatar answered Oct 20 '22 21:10

Frank Schmitt