0

My program converts an alphanumeric phone number into only numbers. For example 1-800-FLOWERS to 18003569377. However, I'm trying to format my output to show 1-800-356-9377.

Heres my code so far:

public static void main(String[] args) 
{
    System.out.println ("Enter phone number:");
    Scanner scanInput = new Scanner (System.in);
    String initialPhoneNumber;
    initialPhoneNumber = scanInput.nextLine ();

    initialPhoneNumber = initialPhoneNumber.toUpperCase();
    long convertedPhoneNumber = phoneNumber (initialPhoneNumber);

    System.out.println ("Converted: " + convertedPhoneNumber);

}

public static long phoneNumber (String initialPhoneNumber)
{
    long number = 0;
    int stringLength = initialPhoneNumber.length();

    for (int digitNum = 0 ; digitNum < stringLength ; digitNum++ )
    {
        char ch = initialPhoneNumber.charAt(digitNum);


        if (Character.isLetter(ch))
        {
            switch(ch)
            {
            case 'A' : case 'B' : case 'C' : number *= 10; number += 2; break;
            case 'D' : case 'E' : case 'F' : number *= 10; number += 3; break;
            case 'G' : case 'H' : case 'I' : number *= 10; number += 4; break;
            case 'J' : case 'K' : case 'L' : number *= 10; number += 5; break;
            case 'M' : case 'N' : case 'O' : number *= 10; number += 6; break;
            case 'P' : case 'Q' : case 'R' : case 'S' : number *= 10; number += 7; break;
            case 'T' : case 'U' : case 'V' : number *= 10; number += 8; break;
            case 'W' : case 'X' : case 'Y' : case 'Z' : number *= 10; number += 9; break;
            }
        }
        else if (Character.isDigit(ch))
        {
            number *= 10; number += Character.getNumericValue(ch);
        }   
    }
    return number;
}   

Any help would be greatly appreceiated!

ahc269
  • 3
  • 1
  • 2

4 Answers4

0

You could use a StringBuilder and something like

StringBuilder sb = new StringBuilder(
        String.valueOf(convertedPhoneNumber));
sb.insert(7, '-');
sb.insert(4, '-');
sb.insert(1, '-');

System.out.println("Converted: " + sb);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

It's probably better to store the phone number as a string but if you store as an integer you can proceed as follows. You can drop digits on the right with / 10 or / 100 or / 1000 ... (integer division). You can drop digits on the left with % 10 or % 100 or % 1000 ... (remainder/modulus). Some combination of those operations will isolate the digits you want.

Edward Doolittle
  • 4,002
  • 2
  • 14
  • 27
0

There is standart format method for Strings. In this example I took substrings and formatted as %s-%s-%s-%.

%s - shows substrings, first %s is first substring, second %s is second substring and etc.

String number = "18003569377";
String formattedNumber = String.format("%s-%s-%s-%s", number.substring(0, 1), number.substring(1, 4), number.substring(4, 7),number.substring(7, 11));

For more details about String.format you can read in this links: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html http://examples.javacodegeeks.com/core-java/lang/string/java-string-format-example/

Anar Orujov
  • 591
  • 5
  • 18
  • 2
    Could you also explain your solution with a couple of sentences? Such answers are always stronger. – Magnilex Apr 19 '15 at 06:39
  • I thought that this is very simple question and the answer is very simple answer and there is no need additional explanation but it turns out that I was wrong. There is standart format method for Strings. In this example I took substrings and formatted as %s-%s-%s-%. %s - shows substrings, first %s is first substring, second %s is second substring and etc. – Anar Orujov Apr 19 '15 at 07:29
  • I edited my answer and added links for more details – Anar Orujov Apr 19 '15 at 07:33
  • 1
    Great. As a rule of thumb, always explain the answer. It might be obvious to you, but perhaps not for a newbie in the area. – Magnilex Apr 19 '15 at 07:52
0

Adding below mentioned in your main method will resolve you issue.

long subString = (convertedPhoneNumber%10000000);    
String updatedStringPhone = initialPhoneNumber.substring(0,6)+subString/10000+"-"+subString%10000;

Overall method will be:

public static void main(String[] args) 
{
    System.out.println ("Enter phone number:");
    Scanner scanInput = new Scanner (System.in);
    String initialPhoneNumber;
    initialPhoneNumber = scanInput.nextLine ();

    initialPhoneNumber = initialPhoneNumber.toUpperCase();
    long convertedPhoneNumber = phoneNumber (initialPhoneNumber);
    long subString = (convertedPhoneNumber%10000000);    
    String updatedStringPhone = initialPhoneNumber.substring(0,6)+subString/10000+"-"+subString%10000;
    System.out.println("Updated: "+updatedStringPhone);
    System.out.println ("Converted: " + convertedPhoneNumber);

}

Description: Converted to strings and added the following strings. For Example
"1-800-"
"356"
"-"
"9377"

comrench
  • 248
  • 2
  • 9