Case statement code error

Hi Experts,

I have written below code to print some message based on i/p. But it is throwing error.
Can someone help to resolve this issue.
def getAgeType(age:Int) = age match{
case (age<10) =>println(“This age belongs to kids”)
case (age>10 && age<=20) =>printl(“This age belongs to teens”)
case (age>20 && age<=60) =>println(“This age belongs to adults”)
case (age>60) =>println(“This age belongs to Oldage”)
}

Regards,
Kumar

First of all age = 10 is missing and you have a printl in place of println. More importantly,

  • enclose your code in triple quotes, so it is readable.
  • “it is throwing an error” is unhelpful, please quote error messages plus context (e.g. a console session)

regards,
Siddhartha

def getAgeType(age: Int):Unit = {
 val message= 
  age match {
  case age:Int if (age < 10) => "This age belongs to kids"
  case age:Int if (10 && age <= 20) => "This age belongs to teens"
  case age:Int if (age > 20 && age <= 60) => "This age belongs to adults"
  case age:Int if (age > 60) => "This age belongs to Oldage"
}
println(message)
}

check how to use pattern guide (if statment before =>) (https://docs.scala-lang.org/tour/pattern-matching.html)
also suggest to use message variable as an intermidiate result
also you named method as GET but it’s return nothig (Unit), so this could be hard to understand later, what do you want from that method

Thanks for your help.
val message=
age match {
case age:Int if (age <= 10) => “This age belongs to kids”
case age:Int if (age > 10 && age <= 20) => “This age belongs to teens”
case age:Int if (age > 20 && age <= 60) => “This age belongs to adults”
case age:Int if (age > 60) => “This age belongs to adults”
}
println(message)
}