Change checking hasNest before nextLine to try-catch section.