Search code examples
kotlinkotlin-extension

Unexpected tokens (use ; to seperate expressions on the same line)


I think I messed up when I tried to write one line code in Kotlin, It seems like there is no problem but IntelliJ gives me this error here:

val cards : Array<Card> = Array(52 { i -> Card(i % 13, getSuit(i))})

Solution

  • You have two ways to fix this error.

    1. Place a , between 52 and the lambda

      val cards : Array = Array(52, { i -> Card(i % 13, getSuit(i))})

    2. Place the lambda outside of the brackets

      val cards : Array = Array(52) { i -> Card(i % 13, getSuit(i))}