简体   繁体   中英

Mapping letters to numbers on phone keyboard

I can't seem to find the problem with this code. I am trying to convert a phone number which is comprised of numbers and letters to numbers only. For example, 1800SMILEING should translate to 1800-764-5464. But my code repeats the last digit in each group. 180048-766-5466 instead of the correct format. It also generates the additional 48 after 800. Please help, I've been working on this for many hours for my Java course homework but I can't figure out the problem.

import java.util.Scanner;

public class PhoneNumberConverter {

    public static void main(String[] args) {
        System.out.println("Enter a phone number to convert:");
        Scanner input = new Scanner(System.in);
        String phoneNumber = input.next();
        
        input.close();
        
        int firstGroup = translatePhoneNumber (phoneNumber, 0, 4);
        System.out.print(firstGroup);
        System.out.print("-");
        
        int secondGroup = translatePhoneNumber (phoneNumber, 4, 6);
        System.out.print(secondGroup);
        System.out.print("-");
        
        int thirdGroup = translatePhoneNumber (phoneNumber, 7, 10);
        System.out.print(thirdGroup);
        
    }
    
    public static int translatePhoneNumber (String phoneNumber, int firstIndex, int lastIndex) {
    
        int chartoNumber = 'A';
        int currentIndex;   
        if (firstIndex != 0) {
        
            for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
                if (phoneNumber.charAt(currentIndex) == 'A' || phoneNumber.charAt(currentIndex) == 'B' || phoneNumber.charAt(currentIndex) == 'C' )
                    chartoNumber = 2;
                else if (phoneNumber.charAt(currentIndex) == 'D' || phoneNumber.charAt(currentIndex) == 'E' || phoneNumber.charAt(currentIndex) == 'F' )
                    chartoNumber = 3;
                else if (phoneNumber.charAt(currentIndex) == 'G' || phoneNumber.charAt(currentIndex) == 'H' || phoneNumber.charAt(currentIndex) == 'I' )
                    chartoNumber = 4;
                else if (phoneNumber.charAt(currentIndex) == 'J' || phoneNumber.charAt(currentIndex) == 'K' || phoneNumber.charAt(currentIndex) == 'L' )
                    chartoNumber = 5;
                else if (phoneNumber.charAt(currentIndex) == 'M' || phoneNumber.charAt(currentIndex) == 'N' || phoneNumber.charAt(currentIndex) == 'O' )
                    chartoNumber = 6;
                else if (phoneNumber.charAt(currentIndex) == 'P' || phoneNumber.charAt(currentIndex) == 'Q' || phoneNumber.charAt(currentIndex) == 'R' || phoneNumber.charAt(currentIndex) == 'S' )
                    chartoNumber = 7;
                else if (phoneNumber.charAt(currentIndex) == 'T' || phoneNumber.charAt(currentIndex) == 'U' || phoneNumber.charAt(currentIndex) == 'V' )
                    chartoNumber = 8;
                else if (phoneNumber.charAt(currentIndex) == 'W' || phoneNumber.charAt(currentIndex) == 'X' || phoneNumber.charAt(currentIndex) == 'Y' || phoneNumber.charAt(currentIndex) == 'Z' )
                    chartoNumber = 9;
                else
                    chartoNumber =  phoneNumber.charAt(currentIndex);
                
                    System.out.print(chartoNumber); 
                    
                }
        } else {
            for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
                chartoNumber =  phoneNumber.charAt(currentIndex);
                char numbeeer = (char) chartoNumber;
                System.out.print(numbeeer);
            }   
        }
        return chartoNumber;    
    }       
}

Firstly note that you are printing out your result in the translatePhoneNumber method itself, you there is not need to print out the result again.

Get rid of System.out.print(firstGroup); etc.

Second, if (firstIndex != 0) { - why? Note that you are doing translatePhoneNumber (phoneNumber, 0, 4); so firstIndex will be 0

Personally I would just pass the substring to the method and loop for each char.

translatePhoneNumber (phoneNumber.substring (0, 4));

....

for (char c : localPhoneNumber) {
    // your mapping
}

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