Replacing a sequence of real numbers with another sequence
Let's split this up a bit; firstly, let's put the sequence filling code in its own function:

def populateSequence(length: Int): IndexedSeq[Double] = {
  for {
    i &lt;- 1 to length
    _ = println("Enter sequence item ? " + i)
    item = readDouble()
  } yield item
}


This utilizes the a combination of for and yield - in this case, this returns an IndexedSeq[Double], but don't worry, you can treat this like a List (or simply convert it to one with a .toList if you really want to.

Our code structure then looks like this:

def main(args: Array[String]) {
  println("Enter Z number: ")
  val z = readDouble();

  println("Enter the length of sequence: ")
  val length = readInt();

  val seq = populateSequence(length) 


Ok, where to from here? Well, in this case, instead of modifying substituteCount as we go, I'd rather just calculate it up front:

val substituteCount = seq count (_ &gt; z)


To get our modified List (or IndexedSequence), we can use another for expression. Again, one thing to remember is that both for and if are expressions in Scala, so we can do this:

val modified = for {
  v &lt;- seq
  item = if (v &gt; z) z else v
} yield item


We can then print everything as usual:

println("Replacement count: " + substituteCount)
println("New sequence: " + modified.mkString(" "))