this problem need print data types https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/B
but he need inputs on just one line with space between each data type
this is my code , what's the problem
import java.util.Scanner
fun main(args:Array){
val scanner = Scanner(System.`in`)
var a:Int=readLine()!!.toInt()
var b:Long=readLine()!!.toLong()
var c:Char=scanner.next().single()
var d:Float=readLine()!!.toFloat()
var e:Double=readLine()!!.toDouble()
println(a)
println(b)
println(c)
println(d)
println(e)
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "45 65896532145 a 45.23 65423.325" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:660) at java.base/java.lang.Integer.parseInt(Integer.java:778) at Test_1Kt.main(test 1.kt:7)
CodePudding user response:
The problem is that you cannot parse the entire input as integer, as the input you expect is set of values with space between. Space itself cannot be parsed into Integer (this is what cause the exception)
To solve this, first, you need to split the input and parse each of the element. You can split it using kotlin built in function, i.e.
val input = readLine() ?: ""
val inputElement = input.split(" ")
Here, the inputElement
is a string array that consist all of element from your input. Next, you need to parse each of the element by the inputElement
index
val a:Int=inputElement[0].toInt()
val b:Long=inputElement[1].toLong()
val c:Char=inputElement[2].single()
val d:Float=inputElement[3].toFloat()
val e:Double=inputElement[4].toDouble()
println(a)
println(b)
println(c)
println(d)
println(e)