I am trying to convert a string to its ascii equivalent using ".toInt", and I am repeatedly getting this error:
java.lang.NumberFormatException: For input string: "'_'"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
java.lang.Integer.parseInt(Integer.java:654)
This is the line it is failing at:
P.map[Expression]{ case y => Chrc(y.toInt)}
Chrc is a parser that returns a string.
I have tried just doing
val x = '_'
println(x.toInt)
outside the block of code I am writing in and it works, so the issue is to do with this sentence and the fact that i am trying to change it to an integer inside the Chrc class. Is there another way to write this?
The problem is reproducible as follows:
Here,
xis aString. That's different from the code you supplied:in which
xis aChar.It is a peculiarity of both Scala and Java that the
Chartype (in Java,charwith a smallc) is an integer type, and thustoIntis a numeric conversion giving you the ASCII value of the character, rather than parsing it.