[英]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.例如,1800SMILEING 应转换为 1800-764-5464。 But my code repeats the last digit in each group.但是我的代码重复了每组中的最后一位数字。 180048-766-5466 instead of the correct format. 180048-766-5466 而不是正确的格式。 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.它还会在 800 之后生成额外的 48。请帮忙,我已经为 Java 课程作业做了很多小时的工作,但我无法弄清楚问题所在。
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.首先请注意,您是在translatePhoneNumber
方法本身中打印出您的结果,您不需要再次打印出结果。
Get rid of System.out.print(firstGroup);
摆脱System.out.print(firstGroup);
etc.等等。
Second, if (firstIndex != 0) {
- why?其次, if (firstIndex != 0) {
- 为什么? Note that you are doing translatePhoneNumber (phoneNumber, 0, 4);
请注意,您正在执行translatePhoneNumber (phoneNumber, 0, 4);
so firstIndex will be 0
所以 firstIndex 将为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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.