Scala Convert a multiline string to BigInt

I am trying to parse 100 50 digit numbers from a line with the below code snippet:

val input = """37107287533902102798797998220837590246510135740250 |46376937677490009712648124896970078050417018260538 |74324986199524741059474233309513058123726617309629""".stripMargin val list = input.split("""\n""").map(BigInt(_)) 

but I end up with "java.lang.NumberFormatException.forInputString (NumberFormatException.java:65)". I do not know why this does not work, since when a line is broken, each element of the list is of type String. Any help would be greatly appreciated.

Best wishes

+4
source share
1 answer

You may be on Windows, where EOL is \ r \ n.

You also need to take this off.

Here I rebooted into Windows to demonstrate ...

 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) 

Note that in cygwin I have to :se fileformat=dos in vi use the line endings \ r \ n.

Here is an example where an unconfirmed impedance mismatch between the source EOL and the EOL runtime listened to me:

some dumb code

You want to use s.lines.mkString , to turn it off.

+1
source

Source: https://habr.com/ru/post/1486044/


All Articles