I get an error. What is the reason? Is my code wrong or is it a problem with the system settings?

57 views Asked by At
package com.company

import  java.util.Scanner

fun main() {
    val reader = Scanner(System.`in`)
       print("Enter your point: ")
    val userInput:Double = reader.nextDouble()
       println("You entered: $userInput")
}

This is my source code

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:947)
    at java.base/java.util.Scanner.next(Scanner.java:1602)
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2573)
    at com.company.MainKt.main(Main.kt:8)
    at com.company.MainKt.main(Main.kt)

I get this error when I run my source code

1

There are 1 answers

0
SM2A On

Mybe you entered a value that is not a number.

Consider cheching you input value.

package com.company

import  java.util.Scanner

fun main() {
    val reader = Scanner(System.`in`)
    print("Enter your point: ")
    val userInput = reader.next().toDoubleOrNull() ?: "Input is not a number"
    println("You entered: $userInput")
}