Advertisement
Guest User

Untitled

a guest
Jul 9th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. String oneLeft(String s) { }
  2.  
  3. private static final Map<Character, Character> LEFT_KEYS = new HashMap<Character, Character>() {{
  4. put('v', 'c');
  5. put('w', 'q');
  6. // etc for all keys that have keys to the left
  7. }};
  8.  
  9. char key; // read from wherever
  10. Character left = LEFT_KEYS.get(key); // could be null
  11.  
  12. public static void main (String[] args) {
  13. Scanner input = new Scanner(System.in);
  14. String key = input.next();
  15. System.out.println(getOneLeftOf(key));
  16. input.close();
  17. }
  18.  
  19. private static char getOneLeftOf(String key) {
  20. String kbd1 = "qwertyuiop[]\";
  21. String kbd2 = "asdfghjkl;'";
  22. String kbd3 = "zxcvbnm,./";
  23. int keyPosition;
  24. char nextToKey = ' ';
  25.  
  26. if (kbd1.contains(key)) {
  27. keyPosition = kbd1.indexOf(key)-1;
  28. nextToKey = kbd1.charAt(keyPosition);
  29.  
  30. } else if (kbd2.contains(key)) {
  31. keyPosition = kbd2.indexOf(key)-1;
  32. nextToKey = kbd2.charAt(keyPosition);
  33.  
  34. } else if (kbd3.contains(key)) {
  35. keyPosition = kbd3.indexOf(key)-1;
  36. nextToKey = kbd3.charAt(keyPosition);
  37.  
  38. } else {
  39. System.out.println("Key not found");
  40. }
  41.  
  42. return nextToKey;
  43. }
  44.  
  45. public class DickTracyRing {
  46.  
  47. public static void main(String[] args) {
  48. char[] encoder=new char['z'+1];
  49. fill(encoder, "qwertyuiop");
  50. fill(encoder, "asdfghjkl");
  51. fill(encoder, "zxcvbnm");
  52. System.out.println(encode("my message", encoder));
  53. }
  54.  
  55. private static String encode(String str, char[] lookup) {
  56. char[] text=str.toCharArray();
  57. for (int i=0; i<text.length; i++) {
  58. if (text[i]>='a' && text[i]<='z') text[i]=lookup[text[i]];
  59. }
  60. return new String(text);
  61. }
  62.  
  63. private static void fill(char[] lookup, String row) {
  64. char[] crow = row.toCharArray();
  65. for (int i = 0; i < crow.length; i++) {
  66. lookup[crow[i]] = crow[(crow.length-1+i)%crow.length];
  67. }
  68. }
  69. }
  70.  
  71. public static char oneLeft (char s)
  72. {
  73. // note the wrap-around chars ('p', 'l', 'm')
  74. final String keyboard = "pqwertyuioplasdfghjklmzxcvbnm";
  75.  
  76. // use lastIndexOf to not conflict with wrap-around chars
  77. int index = keyboard.lastIndexOf(s);
  78.  
  79. return (index > 0) ? keyboard.charAt(index - 1) : s;
  80. }
  81.  
  82. System.out.println(oneLeft('v')); // prints c
  83. System.out.println(oneLeft('z')); // prints m
  84. System.out.println(oneLeft('9')); // prints 9
  85.  
  86. System.out.println(oneLeft('P')); // prints P
  87.  
  88. static String[] lowercaseKeyboard = new String[] {
  89. "`1234567890-=",
  90. "tqwertyuiop[]\",
  91. "asdfghjkl;'n",
  92. "zxcvbnm,./"
  93. };
  94.  
  95. private static char oneLeft(char c) {
  96. for(String s : lowercaseKeyboard) {
  97. int index = s.indexOf(c);
  98.  
  99. if(index>0)
  100. return s.charAt(index-1);
  101. else if(index == 0)
  102. throw new InternalError("there is no char to the left of this");
  103. }
  104.  
  105. throw new InternalError("char entered is not on keyboard");
  106. }
  107.  
  108. oneLeft('s') == 'a'
  109. oneLeft('y') == 't'
  110. oneLeft('c') == 'x'
  111. oneLeft('j') == 'h'
  112. oneLeft(']') == '['
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement