Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Project2 {
- // I changed this to strings because it's easier to work with strings vs characters
- private static String[][] letterRelationships = {{}, //0
- {}, //1
- {"a", "b", "c"}, //2 THIS IS THE ROW
- {"d", "e", "f"}, //3
- {"g", "h", "i"}, //4
- {"j", "k", "l"}, //5
- {"m", "n", "o"}, //6
- {"p", "q", "r", "s"}, //7
- {"t", "u", "v"}, //8
- {"w", "x", "y", "z"}}; //9
- public static void test() {
- }
- public static ArrayList<String[]> getResults(String phoneNum){
- ArrayList<String[]> possibleLetters = new ArrayList<>();
- for (int i = 0; i < phoneNum.length(); i++){
- // Get the first number
- char c = phoneNum.charAt(i);
- // Convert character into String
- String s = Character.toString(c);
- // Convert String into int
- int position = Integer.parseInt(s); // This corresponds to the ROW in your 2D array above
- // Create an array to hold the row
- String[] row = new String[letterRelationships[position].length];
- // Fill the row
- for (int j = 0; j < row.length; j++) {
- row[j] = letterRelationships[position][j];
- }
- // Add this array to the ArrayList
- possibleLetters.add(row);
- /*
- if (phoneNum.charAt(i) == '2'){
- sequence.append(letterRelationships[2]);
- }
- if (phoneNum.charAt(i) == '3'){
- sequence.append(letterRelationships[3]);
- }
- if (phoneNum.charAt(i) == '4'){
- sequence.append(letterRelationships[4]);
- }
- if (phoneNum.charAt(i) == '5'){
- sequence.append(letterRelationships[5]);
- }
- if (phoneNum.charAt(i) == '6'){
- sequence.append(letterRelationships[6]);
- }
- if (phoneNum.charAt(i) == '7'){
- sequence.append(letterRelationships[7]);
- }
- if (phoneNum.charAt(i) == '8'){
- sequence.append(letterRelationships[8]);
- }
- if (phoneNum.charAt(i) == '9'){
- sequence.append(letterRelationships[9]);
- }
- */
- }
- return possibleLetters;
- }
- // Replace hashset with your vector.
- public static HashSet<String> getPossibleWords(ArrayList<String> words, ArrayList<String[]> possibleLetters){
- HashSet<String> results = new HashSet<>();
- // Get the first word in the list
- for (int i = 0; i < words.size(); i++) {
- String aWord = words.get(i);
- // Keep track if the word is legal
- boolean isLegal = true;
- // Traverse the word
- for (int j = 0; j < aWord.length(); j++) {
- // Get the first letter of the word
- String s = Character.toString(aWord.charAt(j)).toLowerCase();
- // Check if the array contains this letter. If it doesn't, word is not legal
- if (!testLetters(s, possibleLetters.get(j))) {
- isLegal = false;
- }
- }
- // Add to array if legal
- if (isLegal) {
- results.add(aWord);
- }
- }
- return results;
- }
- public static boolean testLetters(String letterToTest, String[] row) {
- for (int h = 0; h < row.length; h++) {
- if (letterToTest.toLowerCase().equals(row[h].toLowerCase())) {
- // Is the letter part of the row? Yes, return true
- return true;
- }
- }
- // Reached here, not part of the letter
- return false;
- }
- public static void main(String[] args) {
- // Sample word list
- ArrayList<String> sampleWordStrings = new ArrayList<>();
- sampleWordStrings.add("Perfect");
- sampleWordStrings.add("Tuesday");
- sampleWordStrings.add("Country");
- sampleWordStrings.add("Pumpkin");
- sampleWordStrings.add("Special");
- sampleWordStrings.add("America");
- sampleWordStrings.add("Freedom");
- sampleWordStrings.add("Picture");
- sampleWordStrings.add("Husband");
- sampleWordStrings.add("Monster");
- sampleWordStrings.add("Seventy");
- sampleWordStrings.add("Nothing");
- // Sample Test
- // Sample number
- ArrayList<String[]> myPossibleLetters = getResults("2637422");
- // This should give us the following possibilities
- /*
- * SLOT LETTERS
- * 0 A B C
- * 1 M N O
- * 2 D E F
- * 3 P Q R S
- * 4 G H I
- * 5 A B C
- * 6 A B C
- */
- // Test against the word list
- HashSet<String> myPossibleWords = getPossibleWords(sampleWordStrings, myPossibleLetters);
- // Print the results
- for (String word : myPossibleWords) {
- System.out.println(word);
- }
- // The only word in my small list that can be made is AMERICA so the output should be america
- }
- /*
- public static MyVector compareResults(MyVector words, MyVector results){
- MyVector matches = new MyVector(); //vector to store final (matching) results
- for (int i = 0; i < words.size(); i++){ //so long as there are words in the list
- String currentWord = (String) words.elementAt(i); //convert the word we're on to a String to iterate over
- for (int j = 0; j < currentWord.length(); j++) //iterate over the char of the current word
- for (int k = 2; k < results.size(); k++){ //iterate over the results array elements
- char[] charOfDigit = (char[]) results.elementAt(k); //convert the current sub-array to a single element
- for (int l = 0; l < charOfDigit.length ; l++){ //and iterate over the sub-array elements
- if (currentWord.charAt(j) == charOfDigit[l]) //compare the word char to the digit's char
- matches.append(currentWord); //add matches to the list
- }
- }
- }
- return matches;
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement