take input from file or stdin like Java version