Advertisement
Alekss33

Iteration of String (Letters and Numbers) to collect in hash

May 15th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.*;
  6.  
  7. public class TestIXIII {
  8. public static void test1() {
  9. String input = "1122\n" +
  10. "A1B12C11D2";
  11. System.setIn(new ByteArrayInputStream(input.getBytes()));
  12. }
  13.  
  14. public static void main(String[] args) throws IOException {
  15. test1();
  16. // BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  17. Scanner scanner = new Scanner(System.in);
  18. String secretCode = scanner.nextLine();
  19. String cipher = scanner.nextLine();
  20. TreeMap<Integer, Character> myMap = new TreeMap<>();
  21. Stack<Character> letters = new Stack<>();
  22.  
  23. for (int i = 0; i < cipher.length(); i++) {
  24.  
  25. if (Character.isDigit(cipher.charAt(i))) {
  26. String valueStr = "" + cipher.charAt(i);
  27. while (true) {
  28. i++;
  29. if (i < cipher.length() && Character.isDigit(cipher.charAt(i))) {
  30. valueStr += cipher.charAt(i);
  31. } else {
  32. i--;
  33. break;
  34. }
  35. }
  36.  
  37. int key = Integer.parseInt(valueStr);
  38. char value = letters.pop();
  39. myMap.put(key, value);
  40.  
  41. } else {
  42. letters.add(cipher.charAt(i));
  43. }
  44. }
  45.  
  46. System.out.println(myMap.get(111));
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement