Kotlin(Intellij)中的NumberFormateXception
这是我想做的,我希望用户输入任何数字,并且应该显示它,但是我会遇到此错误。 导入java.util。*
这是我的代码
fun main(){
print("Enter your age")
var age = readLine()!!.toInt()
print(age)
}
,我遇到的错误是
Enter your age 40
Exception in thread "main" java.lang.NumberFormatException: For input string: " 40"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:654)
at java.base/java.lang.Integer.parseInt(Integer.java:784)
at HelloKt.main(Hello.kt:5)
at HelloKt.main(Hello.kt)
Process finished with exit code 1
Here is what I am trying to do, I want the user to enter whatever number and it should be displayed, but I am getting this error.
import java.util.*
This is my code
fun main(){
print("Enter your age")
var age = readLine()!!.toInt()
print(age)
}
And the error I am getting is
Enter your age 40
Exception in thread "main" java.lang.NumberFormatException: For input string: " 40"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:654)
at java.base/java.lang.Integer.parseInt(Integer.java:784)
at HelloKt.main(Hello.kt:5)
at HelloKt.main(Hello.kt)
Process finished with exit code 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在在40的前面编写一个空间,如错误“ 40”所示。
解决方案可能是将所有领先和尾随的空间从阅读线中进行修剪,例如:
You are writing a space in front of the 40 as indicated by the error " 40".
A solution could be is to trim all leading and trailing whitespaces from the readLine, like this: