object TestClass {
def main (args: Array[String]) {
println("Hello World");
val c = List (1,2,3,4,5,6,7,8,9,10)
println(findMax(c))
}
def findMax (tempratures: List[Int]) {
tempratures.foldLeft(Integer.MIN_VALUE) {Math.max}
}
}
Output shown is
Hello World
()
Why is the output not
Hello World
10
I'm doing this in IntelliJ
This is one of the most common scala typos.
You're missing the =
at the end of your method:
def findMax (tempratures: List[Int]) {
Should read:
def findMax (tempratures: List[Int]) = {
Leaving the =
off means that your method returns Unit
(nothing).
Because you define findMax
without return type, so the return type is Unit
or ()
.
def findMax (tempratures: List[Int]) { ... }
aka
def findMax (tempratures: List[Int]) : Unit = { ... }
You want instead
def findMax (tempratures: List[Int]) : Int = { ... }
or with omitted type
def findMax (tempratures: List[Int]) = { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With