There is something not quite right about your using a loop to find entries in a Map. A HashMap is designed to store a key and a value, so you can enter
Campbell Ritchie
385738
and the Map will contain a Map.Entry object representing [Campbell Ritchie ↦ 385738]. What if you want a different Ritchie? [David Ritchie ↦ 385460] That isn't how Maps are supposed to work. Consider a different data representation. You can have a Map<
String, List<String>> instead. You can split the name into two parts and have a mapping like this:-
[Campbell Ritchie ↦ [385738]]
[David Ritchie ↦ [385460]]
[Campbell ↦ [385738]]
[David ↦ [385460]]
[Ritchie ↦ [385738, 385460]]
Now you can use Ritchie or Campbell or David to find the phone numbers; you will also get both phone numbers for both Ritchies. Read more about Maps in the
Java™ Tutorials; there may be an example similar to what I am describing.