Here is my code:

public static void main(String[] args) { Map<Character, Integer> hm = new HashMap<Character, Integer>(); Scanner in=new Scanner(System.in); String User=in.nextLine(); hm.put('a', 1); hm.put('b', 2); hm.put('c', 3); hm.put('d', 4); hm.put('e',5); hm.put('f',6); hm.put('g',7); hm.put('h',8); hm.put('i',9); hm.put('j',10); hm.put('k',11); hm.put('l',12); hm.put('m',13); hm.put('n',14); hm.put('o',15); hm.put('p',16); hm.put('q',17); hm.put('r',18); hm.put('s',19); hm.put('t',20); hm.put('u',21); hm.put('v',22); hm.put('w',23); hm.put('x',24); hm.put('y',25); hm.put('z',26); for (char letter : User.toCharArray()) { for (char key : hm.keySet()) { if (letter == key) { System.out.print(hm.get(key)); System.out.print(" "); } } 

He translates the written text into a digital cipher. How to make so that when entering numbers, the program translates the entered text into letters?

  • Compare by hm.get(key) =) after making Integer.parseInt(тутЧислоВведенноеПользователем); - Alexey Shimansky
  • you have numbers in the map values, then look for the entered numbers in hm.values ​​() and then return their keys. that's just how your code will distinguish the numbers? for example, enter the line "12465"? ... is it 1,2,4,6,5 or, it seems, 12, 46, 5? - SergeiK
  • This is not provided for in the code, but the division between numbers-letters will be a dash. I just do not know exactly how to do it. - Miron Fisenko
  • why is there a map at all? key - 'a' + 1 does the same. - pavel

1 answer 1

Here, it translates the line with numbers into letters, only there are no errors yet, but it copes with the task.

 public class Main { private static Map<Character, Integer> hm = new HashMap<>(); private static Scanner in; public static void main(String[] args) { hm.put('a', 1); hm.put('b', 2); hm.put('c', 3); hm.put('d', 4); hm.put('e', 5); hm.put('f', 6); hm.put('g', 7); hm.put('h', 8); hm.put('i', 9); hm.put('j', 10); hm.put('k', 11); hm.put('l', 12); hm.put('m', 13); hm.put('n', 14); hm.put('o', 15); hm.put('p', 16); hm.put('q', 17); hm.put('r', 18); hm.put('s', 19); hm.put('t', 20); hm.put('u', 21); hm.put('v', 22); hm.put('w', 23); hm.put('x', 24); hm.put('y', 25); hm.put('z', 26); printText(); } private static void printValue() { in = new Scanner(System.in); String user = in.nextLine(); for (char letter : user.toCharArray()) { for (char key : hm.keySet()) { if (letter == key) { System.out.print(hm.get(key)); System.out.print(" "); } } } in.close(); } private static void printText() { System.out.println("вводить цифры через пробел"); in = new Scanner(System.in); String user = in.nextLine(); in.close(); ArrayList<String> strings = new ArrayList<>(); ArrayList<Integer> integers = new ArrayList<>(); char[] elements = user.toCharArray(); String current = ""; for (char element : elements) { if (element == ' ') { strings.add(current); current = ""; } else { current += Character.toString(element); } } strings.add(current); for (String element : strings) { integers.add(Integer.parseInt(element)); } strings.clear(); ArrayList<Character> result = new ArrayList<>(); for (Integer element : integers) { for (Map.Entry<Character, Integer> entry : hm.entrySet()) { if (entry.getValue().equals(element)) { result.add(entry.getKey()); } } } System.out.println(result); } } 

Later I can write more shortly. dirty option in general), but working. Digits are entered through a space.