Help understanding Scanner behavior

Hi all – I’m super new w/Kotlin and I’m trying to understand why I’m getting an error with the following code. For context, I’m working my through the JetBrains’s hyperskill Kotlin course, and I’m curious about the difference between an accepted (i.e. correct) answer and an error when I try to run the same snippet of code locally.

Apologies if you’ve seen this question on the slack channel. I haven’t heard back there and thought someone here may be able to help me understand what’s happening.

Given the following snippet of code:

``` import java.util.Scanner fun main() { println("hello - let's go explore!") val scanner = Scanner(System.`in`) val s1 = scanner.nextLine() val s2 = scanner.nextLine() val num = scanner.nextInt() println(s1) println(s2) println(num) } ``` 

What are the expected (i.e. correct) inputs: a) ABC DEF 123 b) ABC DEF 123 c) 10 ABC 3 d) ABC DEF 123

The correct answer is c and some super helpful users on the slack got me on the right track for thinking about that. However, when I try to run the snippet locally, and supply answer c, I’m getting the following error:

Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at ExploreKt.main(explore.kt:9) at ExploreKt.main(explore.kt)

I’m using IntelliJ 2022.1.1, with OpenJDK 11, and Kotlin 221-1.6.21-release.

Can someone help me understand what’s failing here? Thanks in advance for your time.

submitted by /u/can-of-bees
[link] [comments]