在主要功能(Scala)中无法检测到创建的单例对象

时间:2018-11-09 03:10:10

标签: package singleton main

我创建的单个对象LongLines无法在具有主要功能(FindingLines)的对象中识别。我将它们的文件(LongLines.scala,FindingLines.scala)放在/ src / main / scala / com / files / lines目录中。程序代码必须在特定文件中找到长度大于参数 width 的行。

代码:

1)LongLines.scala(我所学的书的版本):

package com.files.lines
import scala.io.Source

object LongLines {

    def processFile(filename: String, width: Int) {
      val source = Source.fromFile(filename)
      for (line <- source.getLines())
      processLine(filename, width, line)
    }

    private def processLine(filename: String,
      width: Int, line: String) {
      if (line.length > width)
      println(filename +": "+ line.trim)
    }
}

2)LongLines.scala(我自己的版本):

package com.files.lines
import scala.io.Source._
import java.nio.files.Paths._

object LongLines {

   def processFile(filePath: String, width:Int): Unit = {
      val path = get(filePath)
      val fileName = path.getFileName.toString
      val lines = fromFile(filePath).getLines().toList
      for (line<-lines) processLines(fileName,line,width)
   }

   private def processLines(fileName: String, line: String, width: Int): Unit = {
      if (line.length() > width) println(s"$fileName: $line");
   } 
}

3)FindingLines.scala(我使用的图书版本-仅此版本):

package com.files.lines    

object FindLongLines {

  def main(args: Array[String]) = {
    val width = args(0).toInt
    for (arg <- args.drop(1))
        LongLines.processFile(arg, width)
 } 

}


错误(从linux终端编译):

在用我的版本和书籍版本的LongLines.scala编译程序之后(当然,分别是):

  scalac FindingLines.scala 

我遇到此错误:

FindLongLines.scala:15: error: not found: value LongLines
        LongLines.processFile(arg, width)
        ^
one error found

1 个答案:

答案 0 :(得分:0)

解决方案:

1) I removed "package com.file.lines" from both files (FindingLines.scala, LongLines.scala) - that means I need not to remove those files in this package (directory). Those two files could stay in src/main/scala directory.
2) scalac FindingLines.scala LongLines.scala
3) scala FindingLines.scala 45 ~/workspace/Rational/src/Rational.scala