I have a task need to find a particular string in an array: 1. if found, return its value; 2. if not found, return -1.
I wrote an "idea" code, but I don't know how to finish it correctly.
case class person(name:String, value: Int)
personList[Array[person]]
val result = personList match {
case x if x.name == "john" => x.value
case _ => -1 }
the complier reports errors at "case x if x.name"
String. If not found return -1, i.e.Int. That doesn't sound right.StringandIntare different types.