I have a list called contacts which accepts objects of type contact. The method i wrote to add a contact to the list always throws the error "java.util.NoSuchElementException: No line found", when the method is called more than once in a row. Apparently the error stems from the line
name = scanner.nextLine();
I hope someone can point out the problem. Thanks in advance.
public void addNewContact() {
Scanner scanner = new Scanner(System.in);
String name;
int number;
System.out.print("Enter contact name: ");
name = scanner.nextLine();
System.out.print("Enter contact number: ");
number = scanner.nextInt();
scanner.nextLine();
Contact contact = new Contact(name, number);
contacts.add(contact);
scanner.close();
}