ch1maera
ch1maera

Reputation: 1447

Letter to Numeric Phone Number Java

I have the following problem:

Today, everyone comes up with some clever phrase so you can remember their phone number. You have been given the task to decipher these phrases and find out what numbers you need to dial in order to contact these places.
Description: You input will be series of letters, numbers and dashes. You will need to determine the number that the input sequence represents in regular three - dash- four format (see example output). You will also need to determine if the resulting number is a valid number (seven digits) or if there is a number in the input at all.
Input: All letters will be in uppercase. The input string may be up to 25 characters long.
Output: A single line of output is all that is needed either printing the phone number, if the number is not a valid number or if there is no number at all.
Translation Key ABC = 2 DEF = 3 GHI = 4 JKL = 5 MNO = 6 PRS = 7 TUV = 8 WXY = 9 Numbers will be as themselves and ignore all Q, Z and dashes. Example Input: ITS-EASY Example Output : 487-3279
Example Input: ---2---3---TS-4 Example Output : Not a valid number.
Example Input: QZ---I-M-A-TEST Example Output : 462-8378 Example Input: ---------- Example Output : No phone number.

I'm having trouble separating the dashes and unnecessary letters from the actual phrase that translates to the phone number. This is my program so far:

public static void main(String[] args) {
        String cleverPhrase = getCleverPhrase("Input the phrase you use to remember a specific phone number (Max 25 characters allowed): ");
        checkPhrase(cleverPhrase);
    }

    public static String getCleverPhrase(String prompt) {
        String input;
        System.out.print(prompt);
        input = console.nextLine();
        return input;
    }

    public static String checkPhrase(String cleverPhrase) {
        int len = cleverPhrase.length();
        String output = "";
        do {
            for(int i = 0; i <= len; i++) {
                char current = cleverPhrase.charAt(i);

                if (Character.isLetter(current)) {
                    String newCurrent = Character.toString(current);
                    if (newCurrent.equals('Q') || newCurrent.equals('Z')) {

                    }
                }
            }
        } while ()
    }

As you can see, I haven't made much progress. I don't know how to get the program to pick out the unnecessary letters and dashes and return just the letters that form the number. Could someone help me?

Upvotes: 0

Views: 1494

Answers (3)

Joeri Leemans
Joeri Leemans

Reputation: 142

To strip out the unwanted characters in your string, have a look at String.replaceAll

Upvotes: 1

Grzegorz Gajos
Grzegorz Gajos

Reputation: 2363

This method will be very handy in your case. Thanks to that you can replace this

if (current == 'A' || current == 'B' || current == 'C')
...
} else if (current == 'D' || current == 'E' || current == 'F') {
...

with this

StringUtils.replaceChars(input, "ABCDEF", "222333")

You can also get rid of all non-numbers simply by output.replaceAll( "[^\\d]", "" ). At the end, you can add the dash in a specific position and check if the number is valid.

Upvotes: 1

Ramesh-X
Ramesh-X

Reputation: 5055

Check the following code..

public static String checkPhrase(String cleverPhrase) {
    int len = cleverPhrase.length();
    String output = "";

    for (int i = 0; i <= len; i++) {
        char current = cleverPhrase.charAt(i);
        if (Character.isLetter(current)) {
            if (current == 'A' || current == 'B' || current == 'C') {
                output += "2";
            } else if (current == 'D' || current == 'E' || current == 'F') {
                output += "3";
            } else if (...) {
                ....
            }
        }
        if(output.length()==3){
            output += "-";
        }
    }

    if(output.isEmpty()){
        output = "No phone number";
    }else if(output.length()!=8){
        output = "Not a valid number";
    }
    return output;
}

You can extend else-if for all other combinations of numbers. You don't have to check for invalid characters like - or Q or Z. The output variable will be get edited if it goes inside a if statement.

Upvotes: 1

Related Questions