Your solution seems generally sound. I would make a few changes:
You are surrounding your whole code in a try-catch block. Instead, surround the area where the IOException has a chance of being thrown (Also, use System.err to print for errors):
try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); } catch (IOException) { System.err.println("Error"); } And since the in is inside the try-catch and it is required outside, do:
BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader( System.in)); } catch (IOException) { System.err.println("Error"); System.exit(1);return; }