Before reading next line of the Scanner, check whether they exist using hasNextLine() method. So all your below nextLine() should have a hasNextLine() check and the nextInt() should have hasNextInt() check.