12

I'm using Android Studio 3.2 and trying to run a scratch file, but can't find where println is output.

Contents of my scratch file:

fun main(args: Array<String>) {
    println("Hello, world!")
}

But the output window is missing "Hello, world!"

enter image description here

Am I looking in the wrong place?

starkej2
  • 11,391
  • 6
  • 35
  • 41

3 Answers3

14

A .kts file doesn't require a main function. You can add the print statement at the top level.

This would explain the warning about args being unused, since main is never invoked.

A script is a Kotlin source file (.kts) with top level executable code.
Using the command line to run scripts

Salem
  • 13,516
  • 4
  • 51
  • 70
  • 1
    I should still get an output though, no? I tried moving the print statement to the top level and it wasn't working either – starkej2 Sep 27 '18 at 19:49
  • @starkej2 I don't think so, since nothing is actually executed (you just define a function) – Salem Sep 27 '18 at 19:49
  • ah...yeah you're right. Thanks for the help. I didn't notice the .kts file extension and also didn't notice my code was never executing that function. Sometimes another pair of eyes is all you need . – starkej2 Sep 27 '18 at 19:56
1

Simply remove the main function eg: main(args: Array){ } but leave the inside contents of main such as function calls in your scratches file eg: callingFun("PassingString"). Then hit the run button of the scratches file and then output should show up to the right side of the relevant lines of code. screen shot

Michael
  • 21
  • 3
0

Just keep below lines

println("Hello, world!")

What Moira mentioned is correct because function main() is never called.

SRO
  • 11
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 12 '21 at 12:56
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29804144) – M. Dudek Sep 12 '21 at 17:17