https://kotlinlang.org logo
Title
o

Oleh Ponomarenko

11/07/2019, 11:25 AM
How can I transalte this to Kotlin from Java:
while ((inputLine = input.readLine()) != null) {
Log.d(TAG, inputLine);
   }
d

Dennis Schröder

11/07/2019, 1:04 PM
In IntelliJ just past it in and the IDE will convert it for U.
o

Oleh Ponomarenko

11/07/2019, 1:04 PM
unfortunatelly, no
s

Stephan Schroeder

11/07/2019, 1:22 PM
Assuming that there is actually a variable
inputStream
of type InputStream that your BufferedReader
input
is initialized on use:
val inputStream = ...
inputStream.bufferedReader().useLines { lines ->
    for(line in lines) {
        Log.d(TAG, line)
    }
}
This will also close the inputStream.
o

Oleh Ponomarenko

11/07/2019, 1:24 PM
Yes. There should be 2 variables. I'll try your code. Thanks
p

Pavlo Liapota

11/07/2019, 5:01 PM
You can also write it like that if you need while loop 🙂
while (true) {
    val inputLine = input.readLine() ?: break
    Log.d(TAG, inputLine);
}
👍 1
1
o

Oleh Ponomarenko

11/07/2019, 5:02 PM
hm, I like this way)