From b4793b6f60e5b42ba5a06503b17aea917a40306f Mon Sep 17 00:00:00 2001 From: prasanthkumar004 <39922341+prasanthkumar004@users.noreply.github.com> Date: Mon, 4 Jun 2018 11:16:01 -0700 Subject: [PATCH] Add files via upload Did it using Java. Please review the solution and let me know --- onscreenkeyboard.txt | 163 +++++++++++++++++++++++++++++++++++++++++++ testfile.txt | 9 +++ 2 files changed, 172 insertions(+) create mode 100644 onscreenkeyboard.txt create mode 100644 testfile.txt diff --git a/onscreenkeyboard.txt b/onscreenkeyboard.txt new file mode 100644 index 0000000..ea80fbc --- /dev/null +++ b/onscreenkeyboard.txt @@ -0,0 +1,163 @@ +package SoftwritersInt; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Scanner; + +/** + * + * @author PrasanthKumar + */ +public class OnScreenKeyboard { + + OnScreenKeyboard() { + setCharAddr(); + } + private HashMap charAddr = new HashMap<>(); + + /* + ADDING THE KEYBOARD LAYOUT IN A HASHMAP which helps in retreiving the position of the characters + */ + public void setCharAddr() { + this.charAddr.put('A', new int[]{1, 1}); + this.charAddr.put('B', new int[]{1, 2}); + this.charAddr.put('C', new int[]{1, 3}); + this.charAddr.put('D', new int[]{1, 4}); + this.charAddr.put('E', new int[]{1, 5}); + this.charAddr.put('F', new int[]{1, 6}); + this.charAddr.put('G', new int[]{2, 1}); + this.charAddr.put('H', new int[]{2, 2}); + this.charAddr.put('I', new int[]{2, 3}); + this.charAddr.put('J', new int[]{2, 4}); + this.charAddr.put('K', new int[]{2, 5}); + this.charAddr.put('L', new int[]{2, 6}); + this.charAddr.put('M', new int[]{3, 1}); + this.charAddr.put('N', new int[]{3, 2}); + this.charAddr.put('O', new int[]{3, 3}); + this.charAddr.put('P', new int[]{3, 4}); + this.charAddr.put('Q', new int[]{3, 5}); + this.charAddr.put('R', new int[]{3, 6}); + this.charAddr.put('S', new int[]{4, 1}); + this.charAddr.put('T', new int[]{4, 2}); + this.charAddr.put('U', new int[]{1, 3}); + this.charAddr.put('V', new int[]{4, 4}); + this.charAddr.put('W', new int[]{4, 5}); + this.charAddr.put('X', new int[]{4, 6}); + this.charAddr.put('Y', new int[]{5, 1}); + this.charAddr.put('Z', new int[]{5, 2}); + this.charAddr.put('1', new int[]{5, 3}); + this.charAddr.put('2', new int[]{5, 4}); + this.charAddr.put('3', new int[]{5, 5}); + this.charAddr.put('4', new int[]{5, 6}); + this.charAddr.put('5', new int[]{6, 1}); + this.charAddr.put('6', new int[]{6, 2}); + this.charAddr.put('7', new int[]{6, 3}); + this.charAddr.put('8', new int[]{6, 4}); + this.charAddr.put('9', new int[]{6, 5}); + this.charAddr.put('0', new int[]{6, 6}); + + } + + /* + prints the output sequence + accepts the input line from the file as an argument + take each character from the input and finds the positon of the key from the hashmap and compare it with the current position + and append the path of the keys to the output + */ + public StringBuilder printOutputSequence(String input) { + int[] curr_pos = {1, 1};// this is the position of letter A - as cursor initially starts from this position + int[] target_pos = null;//this is the position of the target character + boolean error = false;//if there are any character that is not in the keyboard is encountered this will turn true + StringBuilder output = new StringBuilder(); + for (int i = 0; i < input.length(); i++) { + char searchChar = input.toUpperCase().charAt(i); + if ((searchChar + "").equals(" ")) { + output.append("S,"); + } else { + target_pos = this.charAddr.get(searchChar); + if (target_pos != null) { + int curr_row = curr_pos[0]; + int curr_col = curr_pos[1]; + int target_row = target_pos[0]; + int target_col = target_pos[1]; + int row_diff = curr_row - target_row; + int col_diff = curr_col - target_col; + + if (row_diff > 0) { + for (int j = 0; j < row_diff; j++) { + output.append("U,"); + } + } else if (row_diff < 0) { + for (int j = 0; j < -row_diff; j++) { + output.append("D,"); + } + } + if (col_diff > 0) { + for (int j = 0; j < col_diff; j++) { + output.append("L,"); + } + } else if (col_diff < 0) { + for (int j = 0; j < -col_diff; j++) { + output.append("R,"); + } + } + curr_pos = target_pos; + output.append("#,"); + + } else { + error = true; + } + } + } + //int oLen = output.length(); + output.deleteCharAt(output.length() - 1); + if (error) { + System.out.println("Input has characters that are not available in the keyboard and hence those characters are skipped"); + } + return output; + + } + + /* + GET THE FILE PATH AND RETURNS THE EACH LINE OF THE FILE IN LIST + */ + public ArrayList getWordList(String filePath) { + Scanner s = null; + ArrayList listWords = new ArrayList(); + + try { + s = new Scanner(new File(filePath)); + while (s.hasNext()) { + listWords.add(s.nextLine()); + } + s.close(); + + } catch (FileNotFoundException ex) { + System.out.println("The system cannot find the file specified"); + } catch (Exception e) { + e.printStackTrace(); + } + return listWords; + } + + public static void main(String[] args) { + OnScreenKeyboard osk = new OnScreenKeyboard(); + System.out.println("Enter the full file path of the Input flat file like eg: c:\\myfolder\\flatfile.txt"); + Scanner scanner = new Scanner(System.in); + String filePath = scanner.nextLine(); + ArrayList listWords = osk.getWordList(filePath); + System.out.println("---------------------------------------------------------"); + for (int i = 0; i < listWords.size(); i++) { + System.out.println("INPUT: " + listWords.get(i)); + System.out.println("OUTPUT: " + osk.printOutputSequence(listWords.get(i))); + System.out.println("---------------------------------------------------------"); + } + if (listWords.size() == 0) { + System.out.println("No Input Search Term available in the file"); + System.out.println("---------------------------------------------------------"); + } + } +//C:\Users\PrasanthKumar\Desktop\testfile.txt +} diff --git a/testfile.txt b/testfile.txt new file mode 100644 index 0000000..6b66732 --- /dev/null +++ b/testfile.txt @@ -0,0 +1,9 @@ +'IT CROWD +IT CROWD +&TEST + TEST FILE + SOFT WRITER + SOFT WRITER +ABCDEFGHIJKLmNOPQURSTUVWXYZ +1234567(9890 +12345679890] \ No newline at end of file