Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin scratch file output is missing in Android Studio

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?

like image 795
starkej2 Avatar asked Sep 27 '18 16:09

starkej2


People also ask

How do you run Kotlin on scratch?

From the main menu, select File | New | Scratch File or press Ctrl+Alt+Shift+Insert , then select Kotlin.

How do I run a scratch file in IntelliJ?

From the main menu, select File | New | Scratch File or press Ctrl+Alt+Shift+Insert . Select the language of the scratch file. Scratch files of the same type are automatically numbered and added to the Scratches and Consoles directory of the Project view. in the gutter or press Shift+F10 .

Does Kotlin have a REPL?

Location of REPL in Android Studio: You can find REPL under the Tool -> Kotlin -> Kotlin REPL menu.

What is scratch in Android Studio?

IntelliJ IDEA and Android Studio support Kotlin scratch files and worksheets. Scratch files (or just scratches) let you create code drafts in the same IDE window as your project and run them on the fly.


2 Answers

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

like image 111
Salem Avatar answered Oct 20 '22 13:10

Salem


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

like image 27
Michael Avatar answered Oct 20 '22 11:10

Michael