Issue while writing function in spark

i writing this basic condition for age group

def age (x:Int):Int = {(if x> 18) x else 0}
in spark shell
but getting below error message ::


:1: error: ‘(’ expected but identifier found.
def age (x:Int):Int = {(if x> 18) x else 0}
^
:1: error: illegal start of simple expression
def age (x:Int):Int = {(if x> 18) x else 0}

Can anyone please help me

Thanks in adv

I think you are writing in some other shell.

Could you share your screenshot?

Hi Sir…

the scond image is after entering spark-shell

Thanks

The bracket should be after if not before if.

scala> def age (x:Int):Int = {if (x> 18) x else 0}
def age(x: Int): Int

scala> age(10)
val res0: Int = 0

scala> age(20)
val res1: Int = 20

1 Like

Thank you Sandeep sir…
Its working!!