Rodion Gorkovenko
Rodion Gorkovenko

Reputation: 2852

Creating lazy sequence for file reading in scala

I want to create lazy sequence to read values from input stream in Scala.

import java.util._

object Main {
    val input = new Scanner(System.in)
    def main(args: Array[String]) {
        val it = new MyIt().take(5)

    }
    class MyIt extends Iterator[Int] {
        def hasNext = true
        // def next = 29
        def next = input.nextInt
    }
}

When I changed next = 29 to next = input.nextInt, it would not compile any more complaining that MyIt has no member take. It looks I am completely misunderstanding something. Could you please give me hint (or perhaps link to good article on lazy sequences - there are a lot of results of google, but too much trash, seems - so I am getting lost)

Upvotes: 0

Views: 752

Answers (1)

Sergey Passichenko
Sergey Passichenko

Reputation: 6930

import java.util._ overrides scala.collection.Iterator (which available by default via type alias in scala package object) with java.util.Iterator. Just change your import to import java.util.Scanner

Upvotes: 2

Related Questions