Ben
Ben

Reputation: 751

scala simple while loop error

object JDWhileLoop
{               
                def main(args: Array[String])

                {
                        var index:Int = 0
                        while( index<=10)
                        {

                        println("index="+index)
                        index= index+1              

                        }

                }       
}                   

here is the error

JDWhileLoop.scala:3: error: only classes can have declared but undefined members def main(args: Array[String]) ^

I got this simple code and try to make work,but is not ,I dont know why.please help me. thanks

Upvotes: 0

Views: 272

Answers (1)

om-nom-nom
om-nom-nom

Reputation: 62855

That's formatting error. This should be fine:

object JDWhileLoop
{               
                def main(args: Array[String]) 
                {
                        var index:Int = 0
                        while( index<=10) {
                        println("index="+index)
                        index= index+1           
                        }
                }       
} 

In your code def main(args: Array[String]) treated as an abstract method (without body) followed by some code block in object inner body definition.

Note that in scala the following braces style is preffered

def foo (args: Bar) {
  //some work 
}

Upvotes: 4

Related Questions