ProgramGuru

Read and validate console input

Read and validate console input

A console report can ask for a score, but typed text is not yet a valid mark. Input can end, contain non-numeric words or represent a number outside the accepted range. Handle those cases before allowing the value into report calculations.

What you’ll learn

  • Read, parse and validate console input.
  • Handle invalid text and end of input.

Read and validate console input in the example

  1. Read text

    A console report can ask for a score, but typed text is not yet a valid mark.

    Read text
  2. Parse possible integer

    ReadlnOrNull returns null at end of input.

    Parse possible integer
  3. Validate score range

    With sample input 80 followed by a newline, parsing returns a present Int and range validation succeeds.

    Validate score range
With sample input 80 followed by a newline, parsing returns a present Int and range validation succeeds. The safe calls skip processing if input is absent. Invalid or out-of-range input selects the explanatory alternative.

Understand read and validate console input

ReadlnOrNull returns null at end of input. ToIntOrNull parses a compatible integer string without throwing for invalid text. Presence, parsing and domain validation are separate stages; trimming outer spaces is a deliberate input policy rather than a guarantee about every string.

Connect this to nullable types and safe calls and numbers and explicit conversions.

Follow read and validate console input

Save the complete source as Main.kt and use the Kotlin/JVM environment. Provide sample standard input 80 followed by a newline.

Kotlin/JVM · complete program

fun main() {
  val text = readlnOrNull()
  val score = text?.trim()?.toIntOrNull()
  if (score != null && score in 0..100) println("Score: $score")
  else println("Enter a score from 0 to 100")
}

Expected output

Score: 80

Follow the execution

With sample input 80 followed by a newline, parsing returns a present Int and range validation succeeds. The safe calls skip processing if input is absent. Invalid or out-of-range input selects the explanatory alternative.

Try this in the Kotlin compiler by pasting the complete program.

Practice: Read and validate console input

Your task: What should the program display for input eighty?

Kotlin/JVM · complete program

fun main() {
  val text = readlnOrNull()
  val score = text?.trim()?.toIntOrNull()
  if (score != null && score in 0..100) println("Score: $score")
  else println("Enter a score from 0 to 100")
}

Worked answer

Enter a score from 0 to 100, because numeric parsing returns null and the success condition fails.

Read and validate console input: the important boundary

Readln without the nullable form can fail at end of input. Do not use a fake zero to hide invalid text. Interactive prompts and repeated attempts need a stopping policy when input ends.

Check your understanding

What should the program display for input eighty?
Choose one answer. You can try again.

Answer and explanation

Enter a score from 0 to 100, because numeric parsing returns null and the success condition fails..

Enter a score from 0 to 100, because numeric parsing returns null and the success condition fails.

Read and validate console input: what to keep in mind

Enter a score from 0 to 100, because numeric parsing returns null and the success condition fails. Readln without the nullable form can fail at end of input.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Exceptions and deliberate failure handling →