Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

348
Vistas
How to read all lines from stdin in kotlin?

I know it is possible to use a while loop to fetch a multi-line input from stdin using the readLine function.

Is there a function in kotlin to retrieve all lines at once from stdin without using a JVM api?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

val input = generateSequence(::readLine).joinToString("\n")
print(input)

The first line gets the multi-line input from stdin and put it into a variable.

The generateSequence is a function from kotlin standard library, kotlin.sequences package, that takes another function and calls it until it returns null. In the end it will return an instance of Sequence, that is a sequence of elements that can be iterated over.

The ::readLine part is a way to pass the readLine function, that it is used to read from stdin, to the generateSequence function. Just calling readLine() without :: will cause a compiler error since generateSequence expects for a lambda, not a string.

The joinToString method belongs to the Sequence class. It will iterate over its elements and join them using the given separator, which, in this case, is a new line (\n).

Finnaly at the second line we just print the result to the stdout.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda