Hi all!
I have a basic doubt but I'm stuck with it... How can I continue and accept an empty input? using this:
val scanner = Scanner(System.in)
val myNum = scanner.nextInt()
I must always insert a number for programm to continue. But I wanted to insert nothing and just press enter for the programm to continue. And this way, myNum val would be empty
been googling it, but I think I'm not knowing how to search for it correctly..
m
Milan Hruban
09/10/2020, 12:03 PM
Hi, you probably want nullable integer -
Int?
. By using
nextLine
, feeding newline will cause the scanner to stop reading
Copy code
val scanner = Scanner(System.`in`)
val myNum = scanner.nextLine().toIntOrNull()