[Solved] Phonebook - Java Task (Maps)

  

5
Topic starter

Write a program that receives some info from the console about people and their phone numbers.

You are free to choose the manner in which the data is entered; each entry should have just one name and one number (both of them strings). If you receive a name that already exists in the phonebook, simply update its number.

After filling this simple phonebook, upon receiving the command "search", your program should be able to perform a search of a contact by name and print her details in format "{name} -> {number}". In case the contact isn't found, print "Contact {name} does not exist." Examples:

Examples:

Phonebook Maps - Java Task

3 Answers
4

Here is the solution - read the comments inside the code.

BTW, here I am using regex on line #14 which is (.+)-(.+) Here the dot . means any character and the + means one or more;

This is done to split the input by dash - and devide the input with pattern and matcher into group 1 and group 2

import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Pr_05_Phonebook {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
 
        LinkedHashMap<String, String> map = new LinkedHashMap<>();//CREATING THE MAP FOR PUTTING THE VALUES
 
        String input = "";//EMPTY STRING
 
        Pattern pattern = Pattern.compile("(.+)-(.+)");//USING PATTERN+MATCHER TO DIVIDE THE input TO KEYS AND VALUES - REGEX FOR GROUP 1 + GROUP 2; THE DOT (.) MEANS ANY CHARACTER; THE PLUS SIGN (+) MEANS ONE OR MORE TIMES
 
        while (!(input = sc.nextLine()).equals("search")) {//CHECKING WHETHER THE input is search
            Matcher matcher = pattern.matcher(input);//USING MATCHER
            if (matcher.find()) {
                map.put(matcher.group(1), matcher.group(2));//PUTTING THE ELEMENTS INTO THE map
            }
        }
 
        while (!(input = sc.nextLine()).equals("stop")) {//CHECKING WHETHER THE input is stop
            if (map.containsKey(input)) {
                System.out.printf("%s -> %s\n", input, map.get(input));
            } else {
                System.out.printf("Contact %s does not exist.\n", input);
            }
        }
    }
}
3

To iterate through the keys in a particular TreeMap you can use the following code (see lines #14 and #15):

import java.util.*;
 
public class treeMapExample {
    public static void main(String[] args) {
 
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(9, "fox");
        treeMap.put(4, "bird");
        treeMap.put(5, "aile");
        treeMap.put(6, "lion");
        treeMap.put(7, "hedgehog");
        treeMap.put(8, "elephant");
 
        for (Integer key : treeMap.keySet()) {
            System.out.println(treeMap.get(key));
        }
    }
}
2

Here's one excellent video explaining the sorted maps in Java:

The author creates some very useful videos on YouTube and also has some handy video courses; You can check them here: https://caveofprogramming.com/

Share: