简体   繁体   中英

Loop through multiple arrays - phone number to word generator

I am attempting to write a program that prompts the user to enter a seven-digit telephone number as input and calculates all possible seven-letter word combinations. After sorting the results, I need to print the first and last 10 combinations.

I know that I can individually check all of the inputs with if statements and print them out using a for loop at the end; however, I know that there is a better way to solve this problem.

We are supposed to also make use of the arraycopy() method.

Any ideas/advice would be greatly appreciated! Just looking to improve my coding skills.

EX: Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have three letters associated with them (as seen below). Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use this tool to develop the seven-letter word “NUMBERS.”

2: A B C 
3: D E F 
4: G H I
5: J K L
6: M N 0
7: P R S
8: T U V
9: W X Y

Every seven-letter phone number corresponds to many different seven-letter words, but most of these words represent unrecognizable juxtapositions of letters. It's possible, however, that the owner of a barbershop would be pleased to know that the shop's telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be pleased to know that the number corresponds to the letters “PETCARE.” An automotive dealership would be pleased to know that the dealership number, 639-2277, corresponds to “NEWCARS.”

package Assignment5;
import java.util.Scanner;
import java.lang.System;

public class PhoneNumber 
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String[][] letterArray = {{"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}, {"J", "K", "L"}, {"M", "N", "O"}, {"P", "R", "S"}, {"T", "U", "V"}, {"W", "X", "Y"}};
        char[] numbersAsChar = new char[7];

        System.out.println("Enter phone number: ");
        String input = scan.nextLine();

        //0 and 1 have no alphabetical value
        //If user enters 0 or 1 in phone number - ask them to input a new number
        while(input.contains("0") || input.contains("1") || input.contains(" "))
        {
            System.out.println("Invalid phone number! Please try again without 0 or 1: ");
            input = scan.nextLine();
        }

        //Take each number from user input and assign to the array numbersAsChar
        for(int i = 0; i < input.length(); i++)
        {
            numbersAsChar[i] = input.charAt(i);
        }

        //Print out each individual number input by user - Testing purposes
        for(int i = 0; i < numbersAsChar.length; i++)
            {
                System.out.println("Entered Digit " + (i+1) + ": " + numbersAsChar[i]);
            }

        //Test to see what the value of each character is and print out each letter value
        if (numbersAsChar[0] == '2')
        {
            System.out.println("Hello");
        }


        //use .arraycopy()
    }
}

You could use a recursive function to get all the possible combinations.

This code should help you find all the possible combinations


public static final Map<Integer,String[]> conversionMap = new HashMap<Integer,String[]>();

public static List<String> findCombinations(String phoneNumber, int index, String currentString){

    String currentChar = phoneNumber.substring(index,index+1);

    String[] possibleCharacters = conversionMap.get(Integer.valueOf(currentChar));

    List<String> results = new ArrayList<String>();

    for(String character : possibleCharacters){

        if(!(index == phoneNumber.length()-1)){

            List<String> results2 = findCombinations(phoneNumber,index+1,currentString+character);

            results.addAll(results2);

        }else {
            results.add(currentString+character);
        }

    }

    return results;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM