This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am not sure abt your problem, but i think problem is in System.in argument which is passed to Scanner constructor.
I run the same program using the String as argument to constructor it works fine, same goes for command line arguments
Scanner.hasNext() Returns true if this scanner has another token in its input. This method may block while waiting for input to scan.
Since you specified System.in as the input source for the scanner, the hasNext test returns 'true' unless the input stream is closed. There are no usage of using the hasNext() method here with System.in as the hasNext() doesn't know whether the user *will* input something further or not. In other words, each invocation of next() method requires a lookup on the specified input (here System.in) that causes the user to input something again.