Convert Alphabetic phone number into just numbers

1136 views java
8

I am writing a program in Java that needs to take a numeric phone number from the user, for example: 555-GET-FOOD and then print all the numbers, 555-438-3663.

I ran into some issues because my program just print one number, not all of it. Also, How do I make it that the user can enter dashes as part of their input, for example: 555-GET-FOOD.

This is what I've done so far:

import java.util.*;
public class NumberTranslator {

public static void main(String[] args) {
    // Create Scanner for user input
    Scanner input = new Scanner(System.in);

    // Ask the user to enter the phone number
    System.out.println("Please enter the Phone number in this format: (555-XXX-XXXX) ");

    // Save the phone number into a string
    String phoneNumber = input.nextLine();
    //phoneNumber = phoneNumber.substring(0, 3) + "-" + phoneNumber.substring(3,6)+"-"+phoneNumber.substring(6,10)+"-";

    phoneNumber = phoneNumber.toUpperCase();
    long phoneNumberTranslated = fullPhoneNumber(phoneNumber);

    System.out.println(phoneNumberTranslated);
}

public static long fullPhoneNumber(String phoneNumber) {

    long number = 0;
    int strLength = phoneNumber.length();

    for(int i = 0; i < strLength; i++) {

        char letter = phoneNumber.charAt(i);

        if(Character.isLetter(letter)) {

            switch(letter) {
            case 'A' : case 'B' : case 'C' : number = 2; break;
            case 'D' : case 'E' : case 'F' : number = 3; break;
            case 'G' : case 'H' : case 'I' : number = 4; break;
            case 'J' : case 'K' : case 'L' : number = 5; break;
            case 'M' : case 'N' : case 'O' : number = 6; break;
            case 'P' : case 'Q' : case 'R' : case 'S' : number = 7; break;
            case 'T' : case 'U' : case 'V' : number = 8; break;
            case 'W' : case 'X' : case 'Y' : case 'Z' : number = 9; break;
            }
        }

        else if(Character.isDigit(letter)) {
             Character.getNumericValue(letter);
        }
        else {
            System.out.println("Invalid character!");
        }
    }
    return number;
}
}

The Output I get is as follow:

Please enter the Phone number in this format: (555-XXX-XXXX)
555getfood
3

answered question

2 Answers

7

The issue you have is that you aren't adding to the number - you're overwriting it each time. It would probably be easier to have number be a String and append the corresponding digit to the result string during each iteration of the loop.

Also, in the "else if", you're not doing anything with the expression - you have to store that value in some variable or it doesn't get saved anywhere.

Finally, to let the user input dashes, just add another "else if" where you check if the character is '-', and if it is, you append a dash to the result string.

posted this
8

I suggest that you simply create a map for these

Map<Character, String> numbers = new HashMap <Character, String> ();
numbers.put('A', "1" );
numbers.put('B', "1" );
numbers.put('C', "1" );
numbers.put('D', "2" );
numbers.put('E', "2" );
numbers.put('F', "2" );
// etc

for (char c: phoneNumber.toCharArray()) {
    String val = numbers.get (c);
    if (val == null) val = String.valueOf(c);  // if no mapping use as it is
    System.out.print (val);
}

posted this

Have an answer?

JD

Please login first before posting an answer.