0

您好,谁能帮我解决这个错误,我尝试了很多方法

import scala.io.Source
import java.io._
object test1
{
def main(args: Array[String])
{
    val a=Source.fromFile("pg1661.txt").mkString
    val count=a.split("\\s+").groupBy(x=>x).mapValues(x=>x.length)
    val writer=new PrintWriter(new File("output.txt"))
    writer.write(count)
    writer.close()

}
}

但它显示错误 @write(count) 并且错误是

Multiple markers at this line
- overloaded method value write with alternatives: 
  (java.lang.String)Unit <and> (Array[Char])Unit <and> (Int)Unit cannot be 
  applied to (scala.collection.immutable.Map[java.lang.String,Int])
- overloaded method value write with alternatives: 
  (java.lang.String)Unit <and> (Array[Char])Unit <and> (Int)Unit cannot be 
  applied to (scala.collection.immutable.Map[java.lang.String,Int]) 

请帮助我。在此先感谢

4

2 回答 2

1

如果您想以更多键值样式打印,请尝试

count.foreach{ case (key, value) => writer.println(s"$key: $value") }
于 2013-05-23T09:27:33.837 回答
1
writer.print(count.mkString)

这在将项目写入文件的意义上是有效的

for (i <- count.keys.toList.sorted ) 
     writer.println(i.mkString, count.get(i).mkString)

可能看起来更好一点

于 2013-05-23T08:46:18.280 回答