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);
}
}
}
}