Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

349
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda