The second one reads your entire file in memory, when only one line at a time is enough.
Jon Kho wrote:The function of .hasNext() is to Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.) aka the object has more than 1 attributes right?
1 or more.
Basically, for every call to next(), you should first check hasNext() to see if it would work. The same goes for Scanner's other methods: hasNextInt() should precede nextInt() etc. Only if you're certain there is more you can skip this hasNext() check (like your nextLine() call).