"doesn't work" provides zero information. Does it wander off and make a cup of tea? Does it shut your computer down? Does it generate a black hole?

In any case you can use the Scanner class which...