0

Expectation: console prints: "Making 2 cups of Light coffee"

Reality: Error:(2, 30) Kotlin: The integer literal does not conform to the expected type Array

//Class
class CoffeeMaker(
        var strength: Array<String> = arrayOf("Light", "Medium", "Dark"),
        var cups: Int? = null
) {
    fun brewCoffee() {
        println("Making $cups cups of $strength coffee")
    }
}

// Main.kt
fun main() {
    val coffee = CoffeeMaker(0, 2)
    coffee.brewCoffee()
}
Jordan Tranchina
  • 75
  • 1
  • 1
  • 8

3 Answers3

3

It sounds like you want strength to be an option, for how strong the coffee is, and as such one CoffeeMaker should have just one strength property. An array stores 0 or more things so currently your coffee maker could 0 strengths or a million or whatever.

An Enum represents a single value from a fixed list of options and is probably what you want. You don't need to refer to the item by its index, just the strength itself

enum class CoffeeStrength {  LIGHT, MEDIUM, DARK }

//Class
class CoffeeMaker(
    var strength: CoffeeStrength,
    var cups: Int? = null
) {
    fun brewCoffee() {
        println("Making $cups cups of $strength coffee")
    }
}

// Main.kt
fun main() {
    val coffee = CoffeeMaker(CoffeeStrength.LIGHT, 2)
    coffee.brewCoffee()
}
matt freake
  • 4,877
  • 4
  • 27
  • 56
  • Mmm, an enum looks like a better fit for this sort of problem than [stringly-typed](https://www.techopedia.com/definition/31876/stringly-typed) code; it gives much stronger compile-time checks, easier debugging, and better readability. – gidds Jul 25 '20 at 07:59
  • One clarifying question though, is it possible to convert the text to lowercase? When printed it prints: "Making 2 cups of LIGHT coffee", my hope is to get "Making 2 cups of light coffee" – Jordan Tranchina Jul 27 '20 at 22:50
  • Yes. Kotlin coding standards specify the enums should be in caps, but you can add a property to the enum which contains the String you want to print. For example https://kotlinlang.org/docs/reference/enum-classes.html has an enum Color with an Int property, but it could be a String propety instead – matt freake Jul 28 '20 at 12:09
0

You are passing two integers into the constructor, but your constructor only accepts an Array of Strings and optional an Int.

so what you can do is:

// strength 0. 2 cups
val coffee = CoffeeMaker(arrayOf("0"), 2)

or

// strength 0, 2. null cups
val coffee = CoffeeMaker(arrayOf("0", "2"))

The first argument has to be an array of Strings. To make the 0 (integer) a String you need to double quote it. And then you need to wrap it into an array.

Kotlin doesn't have a literal constructor for arrays like other languages have e.g.: {"0", "2"} or ["0", "2"]

Simulant
  • 19,190
  • 8
  • 63
  • 98
  • Thanks Simulant! I tried using 'fun main() { val coffee = CoffeeMaker(arrayOf("0"), 2) coffee.brewCoffee() }' and didn't get any errors but the console output this: "Making 2 cups of [Ljava.lang.String;@1b28cdfa coffee" Any idea what that may be? – Jordan Tranchina Jul 25 '20 at 07:03
  • you are printing the name of the reference to the array and not the content of the array. have a look here https://stackoverflow.com/q/8410294/1515052 it's the same for Kotlin as for Java. – Simulant Jul 25 '20 at 07:12
0

You are passing the index of the element in the constructor as an argument, you should create a static array in the companion object that stores the information and accept an index of type Int that could be used later to get element at that index from the availableStrength array predefined statically.

class CoffeeMaker(
        var strengthIndex: Int,
        var cups: Int? = null
) {
    fun brewCoffee() {
        println("Making $cups cups of ${availableStrength[strengthIndex]} coffee")
    }

    companion object {
        val availableStrength: Array<String> = arrayOf("Light", "Medium", "Dark")
    }
}
Animesh Sahu
  • 7,445
  • 2
  • 21
  • 49