Java for-each iterating with a Scanner

Usually you want to do something like this, with String.split(…) for(String unit : units.split(",")) { System.out.println("unit: "+unit); } But if for some reason you need the functionality that Scanner affords, and although it’s probably not very readable, you can use an anonymous class to do it like this for (String unit : new Iterable<String>() { [...]