我正在学习用 Scala 编程。我有两个名为chapter3
和的包chapter4
。下面是代码:
FileOperation.scala
包中文件的代码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)
}
}
}
第 4 章包中的文件代码:Summer.scala
package chapter4
import chapter3.FileOperation._
object Summer {
def main(args: Array[String]): Unit = {
{
//val file = new FileOperation
readFile("abc.txt")
}
}
}
当我在 Eclipse 中运行此代码时,它工作正常。但是,当我尝试在终端中编译它时,出现以下错误:
$ 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