Input 控制台Kotlin输入出现问题
代码: 下面是我得到的:Input 控制台Kotlin输入出现问题,input,kotlin,console,console-application,Input,Kotlin,Console,Console Application,代码: 下面是我得到的: 56 -44 -72 -79 -38 -80 4 -85 34 -56 -92 51 36 -12 59 -43 31 17 -76 -8 -52 28 45 -68 -96 -7 -70 20 你可能应该减少你的输入 而不是 Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.f
56 -44 -72 -79 -38 -80 4 -85 34 -56 -92 51 36 -12 59 -43 31 17 -76 -8 -52 28 45 -68 -96 -7 -70 20
你可能应该减少你的输入 而不是
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at KotKt.main(kot.kt:8)
你应该使用
str.split(" ")
String#trim()
方法删除导致错误的前导和尾随空白字符。另外,String#split(RegEx)
方法的RegEx中附加的+
,其效果是,即使使用多个空格作为字符串中数字之间的分隔符,程序仍将正确读取输入,因此不会崩溃
例如,即使此输入也可以工作:
str.trim().split(" +")
str.trim().split(" +")
val str = " 1 2 -54 4 811 33 44 4 321 87 "