Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String oneLeft(String s) { }
- private static final Map<Character, Character> LEFT_KEYS = new HashMap<Character, Character>() {{
- put('v', 'c');
- put('w', 'q');
- // etc for all keys that have keys to the left
- }};
- char key; // read from wherever
- Character left = LEFT_KEYS.get(key); // could be null
- public static void main (String[] args) {
- Scanner input = new Scanner(System.in);
- String key = input.next();
- System.out.println(getOneLeftOf(key));
- input.close();
- }
- private static char getOneLeftOf(String key) {
- String kbd1 = "qwertyuiop[]\";
- String kbd2 = "asdfghjkl;'";
- String kbd3 = "zxcvbnm,./";
- int keyPosition;
- char nextToKey = ' ';
- if (kbd1.contains(key)) {
- keyPosition = kbd1.indexOf(key)-1;
- nextToKey = kbd1.charAt(keyPosition);
- } else if (kbd2.contains(key)) {
- keyPosition = kbd2.indexOf(key)-1;
- nextToKey = kbd2.charAt(keyPosition);
- } else if (kbd3.contains(key)) {
- keyPosition = kbd3.indexOf(key)-1;
- nextToKey = kbd3.charAt(keyPosition);
- } else {
- System.out.println("Key not found");
- }
- return nextToKey;
- }
- public class DickTracyRing {
- public static void main(String[] args) {
- char[] encoder=new char['z'+1];
- fill(encoder, "qwertyuiop");
- fill(encoder, "asdfghjkl");
- fill(encoder, "zxcvbnm");
- System.out.println(encode("my message", encoder));
- }
- private static String encode(String str, char[] lookup) {
- char[] text=str.toCharArray();
- for (int i=0; i<text.length; i++) {
- if (text[i]>='a' && text[i]<='z') text[i]=lookup[text[i]];
- }
- return new String(text);
- }
- private static void fill(char[] lookup, String row) {
- char[] crow = row.toCharArray();
- for (int i = 0; i < crow.length; i++) {
- lookup[crow[i]] = crow[(crow.length-1+i)%crow.length];
- }
- }
- }
- public static char oneLeft (char s)
- {
- // note the wrap-around chars ('p', 'l', 'm')
- final String keyboard = "pqwertyuioplasdfghjklmzxcvbnm";
- // use lastIndexOf to not conflict with wrap-around chars
- int index = keyboard.lastIndexOf(s);
- return (index > 0) ? keyboard.charAt(index - 1) : s;
- }
- System.out.println(oneLeft('v')); // prints c
- System.out.println(oneLeft('z')); // prints m
- System.out.println(oneLeft('9')); // prints 9
- System.out.println(oneLeft('P')); // prints P
- static String[] lowercaseKeyboard = new String[] {
- "`1234567890-=",
- "tqwertyuiop[]\",
- "asdfghjkl;'n",
- "zxcvbnm,./"
- };
- private static char oneLeft(char c) {
- for(String s : lowercaseKeyboard) {
- int index = s.indexOf(c);
- if(index>0)
- return s.charAt(index-1);
- else if(index == 0)
- throw new InternalError("there is no char to the left of this");
- }
- throw new InternalError("char entered is not on keyboard");
- }
- oneLeft('s') == 'a'
- oneLeft('y') == 't'
- oneLeft('c') == 'x'
- oneLeft('j') == 'h'
- oneLeft(']') == '['
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement