Hey there I was trying to make a phone contact project and I started to create a addContact()
void that takes 2 inputs name and phone number in an infinite while loop. My problem is it doesn't take the phone number input after the name input. Can you please help me? Here is my code below
import java.util.*;
public class Problem {
private final Scanner scanner = new Scanner(System.in);
private final ArrayList<String> contactNames = new ArrayList<>();
private final ArrayList<Integer> contactNumbers = new ArrayList<>();
private final HashMap<String, Integer> contactMap = new HashMap<>();
public void addContact() {
while (true) {
System.out.println("Enter a phone number");
int contactNumber = scanner.nextInt();
if (contactNumbers.contains(contactNumber)) {
System.out.println("This number is already given in your contacts");
break;
}
System.out.println();
System.out.println("Enter your contacts name");
String name = scanner.nextLine();
name = name.toLowerCase();
if(contactNames.contains(name)){
System.out.println("This name is already given in your contacts");
break;
}
contactNumbers.add(contactNumber);
contactNames.add(name);
contactMap.put(name, contactNumber);
System.out.println("Person is successfully added to your contacts, here is your all contacts below");
System.out.println(contactMap);
System.out.println("Do you want to quit? -> 1=yes, 2=no");
int quit = scanner.nextInt();
if(quit==1)
break;
}
}
public static void main (String[] args){
Problem a = new Problem();
a.addContact();
}
}