Mike Skyriach wrote:. . . Why is the mooc.fi course teaching me to use Integer.parseInt and not Scanner parse method?
Good question. It is for them to justify why they are teaching that.
What's the difference between those two methods?
It isn't two methods; it is three. Integer.parseInt takes a
String already provided, and Scanner.nextInt takes a file or the keyboard or similar and reads directly to an
int. Note the names of the methods.You need some way to get the String into your program before you can parse it, so you need a preceding method call to get the text to parse.
The parseInt and nextInt methods are related to each other, but they throw different exceptions if your format is wrong. You can catch such exceptions, but you can avoid all Exceptions from the keyboard by combining nextInt and hasNextInt, as Rob Spoor taught me a long time ago.
Lines 3 and 4 may go in either order.
[edit]That code will not throw exceptions as long as System.in remains open. It will not work reliably for reading files.