Just started with Scala. Everytime I try to execute it, a mismatch error is shown. I'm returning integer only. Still the compiler finds type Unit
from god knows where.
object a {
def search(e : Int,arr : Array[Int]): Int = {
var l: Int = 0
for(l <- 0 arr.length) {
if(arr(l) == e) {
return e
} else {
return -1
}
}
}
def main(args: Array[String]) {
var i = 0
println("enter the number of elements ")
var n = readLine.toInt
println("enter the elements are")
var arr = new Array[Int](n)
for(i <- 0 to n-1) {
println("enter element "+i+" ")
arr(i) = readLine.toInt
}
for(i <- 0 to n-1) {
println("element "+i+" is "+arr(i))
}
println("sorted array is")
arr = arr.sortWith(_ < _)
for(i <- 0 to n-1) {
println(arr(i))
}
println("enter the number to be searched ")
var e = readLine.toInt
var result: Int = search(e,arr)
if(result == e) {
println("element found")
} else {
println("element not found")
}
}
}
output
yudhveer@lfc:~$ scalac a.scala
a.scala:6: error: type mismatch;
found : Unit
required: Int
for(l<-0 until arr.length)
^
one error found