This code runs fine with custom input, but error on submitting and with custom input turned off.
Earlier it showed this error:
Exception in thread “main” java.util.NoSuchElementException: No line found
I tried to solve this by adding
if(sc.hasNextLine()), but the problems kept increasing, and I ended up with the current solution.