I am learning to program in Scala. I have two packages named chapter3 and chapter4. Below is the code:
Code for the file FileOperation.scala in package chapter3:
package chapter3
import scala.io.Source
object FileOperation {
/**
* This function determines the length of line length.
*/
def findLineLengthWidth(line : String) : Int = {
val len = line.length.toString.length()
return len;
}
def readFile(filename : String) {
val lines = Source.fromFile(filename).getLines().toList
val longestLine = lines.reduceLeft((a, b) => if(a.length > b.length) a else b)
val maxlength = findLineLengthWidth(longestLine)
for (line <- lines) {
val len = findLineLengthWidth(line)
val spacecount = (maxlength - len)
val padding = " " * spacecount
println(padding + line.length +"|"+ line)
}
}
}
code for file in chapter4 package: Summer.scala
package chapter4
import chapter3.FileOperation._
object Summer {
def main(args: Array[String]): Unit = {
{
//val file = new FileOperation
readFile("abc.txt")
}
}
}
When I run this code in Eclipse, it works fine. However when I try to compile it in the terminal, I get the following error:
$ scalac *.scala
Summer.scala:3: error: not found: object chapter3
import chapter3.FileOperation._
^
Summer.scala:11: error: not found: value readFile
readFile("abc.txt")
^
two errors found