Advertisement
Guest User

Untitled

a guest
May 24th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class P5_Phonebook {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. Scanner console = new Scanner(System.in);
  11. HashMap<String, String> map = new HashMap<>();
  12. Pattern pattern = Pattern.compile("(\\w.*)-(.*)");
  13.  
  14. while (true){
  15. String input = console.nextLine();
  16. if (input.equals("search")){
  17. break;
  18. }
  19.  
  20. Matcher matcher = pattern.matcher(input);
  21. if (matcher.find()) {
  22. String name = matcher.group(1);
  23. String phone = matcher.group(2);
  24. map.put(name, phone);
  25. }
  26. }
  27.  
  28. while (true){
  29. String input = console.nextLine().trim();
  30. if (input.equals("stop")){
  31. break;
  32. }
  33. if (map.containsKey(input)){
  34. System.out.printf("%s -> %s%n", input, map.get(input));
  35. } else {
  36. System.out.printf("Contact %s does not exist.%n", input);
  37. }
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement