Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error : ';' expected but 'else' found

Tags:

scala

I wrote the following simple code:

def Commas(n: Long) = {
  if (n >= 1000)
    Commas(n/1000)
    print(","+ n%1000/100 + n%100/10 + n%10)
  else
    print(n%1000/100 + n%100/10 + n%10)
}

While it seems correct to me, there is an error. What is wrong with the above code?

like image 342
user2206758 Avatar asked May 11 '26 10:05

user2206758


1 Answers

The If...else... syntax expects a statement. You can use a surrounding code block to ensure your code works as expected. Something like(also note that you have to specify return type to Unit or just remove the = sign):

def Commas(n: Long) {
  if (n >= 1000) {
    Commas(n/1000)
    print(","+ n%1000/100 + n%100/10 + n%10)
  }
  else 
    print(n%1000/100 + n%100/10 + n%10)
}
like image 181
korefn Avatar answered May 13 '26 07:05

korefn