The problem has to do with your attempt to open an input stream using System.in, followed by another attempt to open an input stream using System.in without having closed the first stream. Here's a version of your program that doesn't have that problem: