Surender Raja Surender Raja - 2 months ago 6
Scala Question

Scala compile time error while calling a method by passing List[Record]

I am doing some basic handson in scala.. i am getting compile-time error at the place of defining a method called processList.

The compile time error is

Multiple markers at this line:
- only classes can have declared but undefined
members


My code is as follows,

package pack1

import scala.io.Source

case class Record(id:Int, name:String, city:String)

object ReadingFile {

def main(args: Array[String]): Unit = {
val fileLoc:String = "/home/edureka/surender/inputfiles/records.txt"

val fileData:List[String] = Source.fromFile(fileLoc).getLines().toList

val fileList =fileData.map { eachLine => {
val Array(a:String,b:String,c:String) = eachLine.split(",")
Record(a.toInt,b,c)
} }

println(fileList)

processList(fileList)

}

def processList(myList:List[Record])

{

}

}


I dont know how to fix this, Can somebody help me on this?

Answer

Because of the new line after def processList(myList:List[Record]) Scala compiler thinks that the method implementation is not given and below block {} does not belong to the method. So compiler thinks its a abstract method.

Declare processList like this

 def processList(myList:List[Record]) {

 }

or

 def processList(myList:List[Record]): Unit = {

 }

Instead of this

def processList(myList:List[Record])
                                     //remove this line to fix the error
   {

   }