问题描述
我正在尝试使用以下代码从字符串中解析 100 个 50 位数字:
I'm trying to parse 100 50-digit numbers from string with below piece of code:
val input = """37107287533902102798797998220837590246510135740250
|46376937677490009712648124896970078050417018260538
|74324986199524741059474233309513058123726617309629""".stripMargin
val list = input.split("""\n""").map(BigInt(_))
但我以java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)"结尾.我不知道为什么这不起作用,因为当字符串被拆分时,列表的每个元素都是字符串类型.任何帮助将不胜感激.
but I'm ending with "java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)". I don't have any idea why this does not work, since when string is splited, each element of list is of type String. Any help would be much appreciated.
最好的问候
推荐答案
可能您使用的是 Windows,EOL 是 \r\n.
Probably you're on Windows, where the EOL is \r\n.
你也必须剥掉它.
在这里,我重新启动到 Windows 进行演示...
Here, I rebooted into Windows to demo...
apm@halyard ~/tmp
$ vi bigbomb.scala
apm@halyard ~/tmp
$ skalac bigbomb.scala ; skala bigbomb.Test
"ava.lang.NumberFormatException: For input string: "35740250
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
请注意,在 cygwin 中,我必须在 vi 中 :se fileformat=dos
以使用 \r\n 行结尾.
Note that in cygwin, I have to :se fileformat=dos
in vi to use the \r\n line endings.
这是一个示例,其中源 EOL 和运行时 EOL 之间未确认的阻抗不匹配使我烦恼:
Here is an example where the unacknowledged impedance mismatch between the source EOL and the runtime EOL bugged me:
你想用 s.lines.mkString
把它去掉.
这篇关于Scala 将多行字符串转换为 BigInt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!