Throws Exception with Scanner

If I do not use the while loop while(sc.hasNext()) the compiler throws excetion at scanner . But the code runs smoothly. Why is it so?

why do you want to use while(sc.hasNext()) in your code? it is specified that you have to print n1 terms of a given series which are not divisible by n2…so technically you have to take only 2 inputs n1 and n2…

This code works fine in ide.codingblocks.com but when I take input without using th loop while(sc.hasNext(()) it throws exception

i dont think there is an issue…your code is perfectly running on hackerblocks…and my ques still remains the same why do you want to use while(sc.hasNext()) in your code?