Scala初学问题

Scala初学问题

今天刚刚接触 Scala, 以下是我在试验书本代码时遇到的问题

import scala.io.Source

def widthOfLength(s : String) = s.length.toString.length
if(args.length > 0){
	val lines = Source.fromFile(args(0)).getLines.toList
	val longesLine = lines.reduceLeft(
		(a, b) => if(a.length > b.length) a else b
	)
	val maxWidth = widthOfLength(longesLine)
	for(line <- lines){
		val numSpaces = maxWidth - widthOfLength(line)
		val padding = " " * numSpaces
		println(padding + line.length +" | "+line)
	}
}
	else
		Console.err.println("Please enter filename")

控制台报错

E:\>scala ioStream.scala ioStream.scala
E:\ioStream.scala:11: error: value widthOfLength is not a member of Int
                val numSpaces = maxWidth widthOfLength(line)
                                         ^
one error found

我尝试将函数输出类型定义为 Int 却没有效果,记录下,日后填坑
已修改这是一个符号引起的 ,好尴尬
贴出正确运行的结果

23 | import scala.io.Source
 1 |
56 | def widthOfLength(s : String) = s.length.toString.length
20 | if(args.length > 0){
53 |    val lines = Source.fromFile(args(0)).getLines.toList
35 |    val longesLine = lines.reduceLeft(
44 |            (a, b) => if(a.length > b.length) a else b
 2 |    )
41 |    val maxWidth = widthOfLength(longesLine)
20 |    for(line <- lines){
48 |            val numSpaces = maxWidth - widthOfLength(line)
31 |            val padding = " " * numSpaces
44 |            println(padding + line.length +" | "+line)
 2 |    }
 1 | }
 5 |    else
46 |            Console.err.println("Please enter filename")

我的博客站

转载于:https://my.oschina.net/dasven/blog/863650

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值