less than 1 minute read

I have taken an algorithms course on coursera 1 several times. It is a good coding kata 2!

On my most recent shuffle through, I encountered yuck during setup. On the hello world, it has you create a simple java program and then read in values from a custom StdIn package. The expectation is to use code similiar to:

  while (!StdIn.isEmpty()) {
      double value = StdIn.readString();
      StdOut.println(value);
  }

Then, you will read from terminal or text file.

The issue is when you read from terminal, StdIn.isEmpty() blocks waiting for input, and thus the program never terminates. On my Mac, I have to manually ^D 3 to cancel.

My solution is to either use with text files or pipe a set of strings to the program, which works fine. Just don’t try to manually process through interactive prompt.

% javac-algs4 RandomWord.java       
% echo "a b" | java-algs4 RandomWord 
a
b
% java-algs4 RandomWord < animals.txt
bass
bear
boar
bull

  1. Algorithms from Princeton University https://www.coursera.org/learn/algorithms-part1/home 

  2. Coding kata is an exercise of simple repeated exercises to hone your skills https://en.wikipedia.org/wiki/Kata 

  3. ^D (CTRL-D) on Mac, CTRL-Z on Windows send SIGINT to the current foreground process, which terminates the application