Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.16 KB | None | 0 0
  1. public class Project2 {
  2.    
  3.     // I changed this to strings because it's easier to work with strings vs characters
  4.     private static String[][] letterRelationships = {{},  //0
  5.                                                    {},  //1
  6.                                                    {"a", "b", "c"},  //2 THIS IS THE ROW
  7.                                                    {"d", "e", "f"},  //3
  8.                                                    {"g", "h", "i"},  //4
  9.                                                    {"j", "k", "l"},  //5
  10.                                                    {"m", "n", "o"},  //6
  11.                                                    {"p", "q", "r", "s"}, //7
  12.                                                    {"t", "u", "v"},  //8
  13.                                                    {"w", "x", "y", "z"}}; //9
  14.    
  15.     public static void test() {
  16.        
  17.     }
  18.    
  19. public static ArrayList<String[]> getResults(String phoneNum){
  20.     ArrayList<String[]> possibleLetters = new ArrayList<>();
  21.     for (int i = 0; i < phoneNum.length(); i++){
  22.        
  23.         // Get the first number
  24.         char c = phoneNum.charAt(i);
  25.        
  26.         // Convert character into String
  27.         String s = Character.toString(c);
  28.        
  29.         // Convert String into int
  30.         int position = Integer.parseInt(s); // This corresponds to the ROW in your 2D array above
  31.        
  32.         // Create an array to hold the row
  33.         String[] row = new String[letterRelationships[position].length];
  34.        
  35.         // Fill the row
  36.         for (int j = 0; j < row.length; j++) {
  37.             row[j] = letterRelationships[position][j];
  38.         }
  39.        
  40.        
  41.        
  42.         // Add this array to the ArrayList
  43.         possibleLetters.add(row);
  44.        
  45.        
  46.         /*
  47.         if (phoneNum.charAt(i) == '2'){
  48.             sequence.append(letterRelationships[2]);
  49.         }
  50.         if (phoneNum.charAt(i) == '3'){
  51.             sequence.append(letterRelationships[3]);
  52.         }
  53.         if (phoneNum.charAt(i) == '4'){
  54.             sequence.append(letterRelationships[4]);
  55.         }
  56.         if (phoneNum.charAt(i) == '5'){
  57.             sequence.append(letterRelationships[5]);
  58.         }
  59.         if (phoneNum.charAt(i) == '6'){
  60.             sequence.append(letterRelationships[6]);
  61.         }
  62.         if (phoneNum.charAt(i) == '7'){
  63.             sequence.append(letterRelationships[7]);
  64.         }
  65.         if (phoneNum.charAt(i) == '8'){
  66.             sequence.append(letterRelationships[8]);
  67.         }
  68.         if (phoneNum.charAt(i) == '9'){
  69.             sequence.append(letterRelationships[9]);
  70.         }
  71.         */
  72.     }
  73.        
  74.            
  75.     return possibleLetters;
  76. }
  77.  
  78. // Replace hashset with your vector.
  79. public static HashSet<String> getPossibleWords(ArrayList<String> words, ArrayList<String[]> possibleLetters){
  80.     HashSet<String> results = new HashSet<>();
  81.    
  82.     // Get the first word in the list
  83.     for (int i = 0; i < words.size(); i++) {
  84.         String aWord = words.get(i);
  85.        
  86.         // Keep track if the word is legal
  87.         boolean isLegal = true;
  88.        
  89.         // Traverse the word
  90.         for (int j = 0; j < aWord.length(); j++) {
  91.             // Get the first letter of the word
  92.             String s = Character.toString(aWord.charAt(j)).toLowerCase();
  93.            
  94.             // Check if the array contains this letter. If it doesn't, word is not legal
  95.             if (!testLetters(s, possibleLetters.get(j))) {
  96.                 isLegal = false;
  97.             }
  98.         }
  99.        
  100.         // Add to array if legal
  101.         if (isLegal) {
  102.             results.add(aWord);
  103.         }
  104.     }
  105.    
  106.    
  107.     return results;
  108. }
  109.  
  110. public static boolean testLetters(String letterToTest, String[] row) {
  111.    
  112.     for (int h = 0; h < row.length; h++) {
  113.         if (letterToTest.toLowerCase().equals(row[h].toLowerCase())) {
  114.             // Is the letter part of the row? Yes, return true
  115.             return true;
  116.         }
  117.  
  118.     }
  119.    
  120.     // Reached here, not part of the letter
  121.     return false;
  122. }
  123.  
  124. public static void main(String[] args) {
  125.    
  126.     // Sample word list
  127.     ArrayList<String> sampleWordStrings = new ArrayList<>();
  128.     sampleWordStrings.add("Perfect");
  129.     sampleWordStrings.add("Tuesday");
  130.     sampleWordStrings.add("Country");
  131.     sampleWordStrings.add("Pumpkin");
  132.     sampleWordStrings.add("Special");
  133.     sampleWordStrings.add("America");
  134.     sampleWordStrings.add("Freedom");
  135.     sampleWordStrings.add("Picture");
  136.     sampleWordStrings.add("Husband");
  137.     sampleWordStrings.add("Monster");
  138.     sampleWordStrings.add("Seventy");
  139.     sampleWordStrings.add("Nothing");
  140.    
  141.     // Sample Test
  142.    
  143.     // Sample number
  144.     ArrayList<String[]> myPossibleLetters = getResults("2637422");
  145.     // This should give us the following possibilities
  146.     /*
  147.      * SLOT         LETTERS
  148.      * 0            A B C
  149.      * 1            M N O
  150.      * 2            D E F
  151.      * 3            P Q R S
  152.      * 4            G H I
  153.      * 5            A B C
  154.      * 6            A B C
  155.      */
  156.    
  157.     // Test against the word list
  158.     HashSet<String> myPossibleWords = getPossibleWords(sampleWordStrings, myPossibleLetters);
  159.    
  160.     // Print the results
  161.     for (String word : myPossibleWords) {
  162.         System.out.println(word);
  163.     }
  164.    
  165.     // The only word in my small list that can be made is AMERICA so the output should be america
  166.    
  167. }
  168.  
  169. /*
  170. public static MyVector compareResults(MyVector words, MyVector results){
  171.     MyVector matches = new MyVector();                              //vector to store final (matching) results
  172.     for (int i = 0; i < words.size(); i++){                         //so long as there are words in the list
  173.         String currentWord = (String) words.elementAt(i);           //convert the word we're on to a String to iterate over
  174.         for (int j = 0; j < currentWord.length(); j++)              //iterate over the char of the current word
  175.             for (int k = 2; k < results.size(); k++){               //iterate over the results array elements
  176.             char[] charOfDigit = (char[]) results.elementAt(k);     //convert the current sub-array to a single element
  177.                 for (int l = 0; l < charOfDigit.length ; l++){      //and iterate over the sub-array elements
  178.                     if (currentWord.charAt(j) == charOfDigit[l])    //compare the word char to the digit's char
  179.                         matches.append(currentWord);                //add matches to the list
  180.                 }
  181.             }
  182.         }
  183.     return matches;
  184.     }
  185.     */
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement