0

When running the following code as a snippet from https://www.programiz.com/kotlin-programming/examples/convert-list-array :

   
    // printing elements of the array list
    vowels_list.forEach { System.out.print(it) }


   // vowels array
    val vowels_array: Array<String> = arrayOf("a", "e", "i", "o", "u")
    
    // converting array to array list
    val vowels_list: List<String> = vowels_array.toList()
    
    // printing elements of the array list
    vowels_list.forEach { System.out.print(it) }

The output is supposed to be aeiou

However when I am running it in the kotlin REPL nothing is printed. Why is that?

Update There seems to be something going on for the kotlin repl itself. I just noticed there is a bracket - as if the code were not completed yet:

![enter image description here

I have to hit CTL-C and then we see this:

aeiou<interrupted>

What is happening here?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • The above code works fine for me in Kotlin REPL. I don't it's a problem with the code. It is probably an issue with your IDEA. This makes no sense but can you remove the comments and try again. It's a long shot – Xid Jan 17 '21 at 20:29
  • I'm running this in the REPL - which is noted in the question. I'll add that to the title since it seems to be the real problem – WestCoastProjects Jan 17 '21 at 20:31
  • The issue might be in the buffering of the terminal where you are running REPL. Try to add `println()` to the end of your code. – Konstantin Raspopov Jan 17 '21 at 20:36
  • @KonstantinRaspopov OK yes that solved it. Please make that an answer – WestCoastProjects Jan 17 '21 at 20:44

2 Answers2

2

Did you try to run only the last three lines of code?
I did it by running on https://play.kotlinlang.org/ and it printed aeiou.

DuDa
  • 3,718
  • 4
  • 16
  • 36
2

The issue is in the buffering of the terminal. You may add println() to the end of your code to force flushing the symbols to the screen.

Konstantin Raspopov
  • 1,565
  • 1
  • 14
  • 20
  • btw Did you run into any way to disable output buffering in the REPL? – WestCoastProjects Jan 17 '21 at 20:59
  • @StephenBoesch as far as I know, there's no easy way to do it. Also, it depends on your OS. You can try some approaches from this question https://stackoverflow.com/q/3465619/2314529 – Konstantin Raspopov Jan 17 '21 at 21:24
  • This isn't specifically an REPL issue.  It's very common for text output to be line-buffered; stdout is on most operating systems (though not stderr).  So it's good practice to write a trailing newline character (e.g. by using `println()`).  In fact, on Unix a text file is always assumed to end with a newline, and some tools may ignore anything after the last newline. – gidds Jan 17 '21 at 22:52