0

It is simple probably but I have no idea how to make it. Help The problem is at the end of the code.

*****I would like to have a result like this 556668 and I've got like this:

[2, 22, 222, 3, 33, 333, 4, 44, 444, 5, 55, 555, 6, 66, 666, 7, 77, 777, 7777, 8, 88, 888, 9, 99, 999, 9999][2, 22, 222, 3, 33, 333, 4, 44, 444, 5, 55, 555, 6, 66, 666, 7, 77, 777, 7777, 8, 88, 888, 9, 99, 999, 9999][2, 22, 222, 3, 33, 333, 4, 44, 444, 5, 55, 555, 6, 66, 666, 7, 77, 777, 7777, 8, 88, 888, 9, 99, 999, 9999]**


My code:

changingLettersOnNumbersAndMap("KOT");



static public void changingLettersOnNumbersAndMap(String letters) {
    Map<Character, String> map = new HashMap<>();
    String numbers = "";

    map.put('A', "2");
    map.put('B', "22");
    map.put('C', "222");
    map.put('D', "3");
    map.put('E', "33");
    map.put('F', "333");
    map.put('G', "4");
    map.put('H', "44");
    map.put('I', "444");
    map.put('J', "5");
    map.put('K', "55");
    map.put('L', "555");
    map.put('M', "6");
    map.put('N', "66");
    map.put('O', "666");
    map.put('P', "7");
    map.put('Q', "77");
    map.put('R', "777");
    map.put('S', "7777");
    map.put('T', "8");
    map.put('U', "88");
    map.put('V', "888");
    map.put('W', "9");
    map.put('X', "99");
    map.put('Y', "999");
    map.put('Z', "9999");


    for (int i = 0; i < letters.length(); i++) {
        char charAt = letters.charAt(i);
        if (map.containsKey(charAt)) {
            Collection<String> values = map.values();

            Set<Map.Entry<Character, String>> entries = map.entrySet();
            numbers = numbers + entries.stream().map(Map.Entry::getValue).collect(Collectors.toList());
        }
    }

    System.out.println(numbers);
}
WJS
  • 36,363
  • 4
  • 24
  • 39
Pp1
  • 41
  • 3
  • 1
    Can you clarify which `"three key"`, where they come from? Please consider [*editing the question*](https://stackoverflow.com/posts/73392143/edit) by explaining what you're trying to achieve. – Alexander Ivanchenko Aug 17 '22 at 16:46
  • Remove anything inside the if-block and append only the string from the map there: `numbers = numbers + map.get(charAt);` – Mihe Aug 17 '22 at 16:49
  • You have not explained nearly enough for someone to adequately help. We should not have to look at your code to figure out what you are trying to do. Please provide more detail. – WJS Aug 17 '22 at 16:55
  • @Mihe thanks!!! it works I knew it was simple :) – Pp1 Aug 17 '22 at 16:56
  • 1
    You tagged your question java-stream. So: `String numbers = letters.chars() .mapToObj(ch -> Character.valueOf((char) ch)) .map(map::get) .filter(Objects::nonNull) .collect(Collectors.joining());`. Gives `556668`. – Ole V.V. Aug 17 '22 at 18:11

1 Answers1

1
  • use a StringBuilder to house the result.
  • then simply append the value for the required character.
String result = changingLettersOnNumbersAndMap("KOT");
System.out.println(result);

prints

556668



static public String changingLettersOnNumbersAndMap(String letters) {
   Map<Character, String> map = new HashMap<>();

   map.put('A', "2");
   map.put('B', "22");
   map.put('C', "222");
   map.put('D', "3");
   map.put('E', "33");
   map.put('F', "333");
   map.put('G', "4");
   map.put('H', "44");
   map.put('I', "444");
   map.put('J', "5");
   map.put('K', "55");
   map.put('L', "555");
   map.put('M', "6");
   map.put('N', "66");
   map.put('O', "666");
   map.put('P', "7");
   map.put('Q', "77");
   map.put('R', "777");
   map.put('S', "7777");
   map.put('T', "8");
   map.put('U', "88");
   map.put('V', "888");
   map.put('W', "9");
   map.put('X', "99");
   map.put('Y', "999");
   map.put('Z', "9999");


   StringBuilder numbers = new StringBuilder();
   for (int i = 0; i < letters.length(); i++) {
       char charAt = letters.charAt(i);
       if (map.containsKey(charAt)) {
            numbers.append(map.get(charAt));
       }
    }

    return numbers.toString();
       
}

You would be better off creating the Map outside the method and passing it as an argument. That way it only needs to be created once,.

WJS
  • 36,363
  • 4
  • 24
  • 39