# Checking for valid entries

**URL:** <https://discuss.kotlinlang.org/t/checking-for-valid-entries/4132>\
**Category:** Support\
**Created:** [August 11, 2017, 12:05am UTC](https://discuss.kotlinlang.org/t/checking-for-valid-entries/4132 "2017-08-11T00:05:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lottjohn19](https://avatars.discourse-cdn.com/v4/letter/l/6f9a4e/32.png) [@lottjohn19](https://discuss.kotlinlang.org/u/lottjohn19)\
**Post date:** [August 11, 2017, 12:05am UTC](https://discuss.kotlinlang.org/t/checking-for-valid-entries/4132/1 "2017-08-11T00:05:04Z")

</div>

Hello,

I am learning more and more about Kotlin every day. I have found myself facing another interesting roadblock.

Using the code below, I want to write a check that verifies if the user actually entered a number. If not, I want to go back and ask them to enter again. If they DID enter a number, I want to value to proceed into the calculateTotal function. How would I check if an entry matches the criteria? Thanks in advance.

> ```
> print("Enter the total of your bill : ")
> var originalTotal: Double?
> originalTotal = readLine()!!.toDouble()
> 
> ```

> ```
> if(originalTotal !is Double) {
> println("You did not enter a valid entry:")
> 
> } else {
> val taxTotal = calculateTotal(originalTotal)
> println("Your total with standard tip is: $taxTotal")
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![abond](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@abond](https://discuss.kotlinlang.org/u/abond)\
**Post date:** [August 11, 2017, 5:28am UTC](https://discuss.kotlinlang.org/t/checking-for-valid-entries/4132/2 "2017-08-11T05:28:06Z")

</div>

> [@Setting a User Input to a Double (Or a Specific Data Type)](https://discuss.kotlinlang.org/t/setting-a-user-input-to-a-double-or-a-specific-data-type/4082/2):
>
> Try this instead which checks that what you’re entering is a valid number and, if it isn’t, asks you to reinput it: var originalTotal: Double? do { print("Enter the original total : ") originalTotal = readLine()!!.toDoubleOrNull() if (originalTotal == null) println("Not a valid number, try again") } while (originalTotal == null) var tip = 0.15 \* originalTotal println("Your tip is : $tip") var total = originalTotal + tip println("Your total…

---

<div class="post-metadata">

**Author:** ![lottjohn19](https://avatars.discourse-cdn.com/v4/letter/l/6f9a4e/32.png) [@lottjohn19](https://discuss.kotlinlang.org/u/lottjohn19)\
**Post date:** [August 11, 2017, 4:10pm UTC](https://discuss.kotlinlang.org/t/checking-for-valid-entries/4132/3 "2017-08-11T16:10:17Z")

</div>

The following is what I have. It only runs the first println and then gives the following error:

Exception in thread “main” kotlin.KotlinNullPointerException  
at Simplest\_versionKt.main(Simplest version.kt:10)

```
fun main(args: Array<String>) {
    var originalTotal: Double?
    do {
        println("Please enter the total of your bill: ")
        originalTotal= readLine()!!.toDoubleOrNull()
        if(originalTotal == null){
            println("Not a valid number, try again")
        }
    }while(originalTotal==null)
    
    var tip = 0.15 * originalTotal
    println("Your tip is: $tip")
    var total = originalTotal + tip
    println("Your total is: $total")
}

```

---

<div class="post-metadata">

**Author:** ![Eliote](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/eliote/32/7934_2.png) [@Eliote](https://discuss.kotlinlang.org/u/Eliote)\
**Post date:** [August 11, 2017, 5:51pm UTC](https://discuss.kotlinlang.org/t/checking-for-valid-entries/4132/4 "2017-08-11T17:51:52Z")

</div>

What about a function that only returns if the readLine() get a not null transformation:

```
fun <T> readLineNotNull(transformation: (String) -> T): T =
	transformation(readLine() ?: "")?.let { return it } ?: readLineNotNull(transformation)

```

then use it like:

```
val myDouble = readLineNotNull { it.toDoubleOrNull() } // read line until a Double is entered
val notOne = readLineNotNull { if (it == "1") null else it.toDoubleOrNull() } // read line until it's diferent from "1" and is a Double

```

---

<div class="post-metadata">

**Author:** ![alanfo](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alanfo](https://discuss.kotlinlang.org/u/alanfo)\
**Post date:** [August 11, 2017, 5:54pm UTC](https://discuss.kotlinlang.org/t/checking-for-valid-entries/4132/5 "2017-08-11T17:54:57Z")

</div>

It runs fine on my machine - here’s some sample output:

```auto
Please enter the total of your bill: 
100
Your tip is: 15.0
Your total is: 115.0

```

It’s difficult to see what could be null here as originalTotal must be a number after the do/while loop ends.

I’d scrub what you have, try pasting the above code in again and see if the result is the same.
