【问题标题】:Letter to Numeric Phone Number Java给数字电话号码的字母 Java
【发布时间】:2016-03-21 11:54:03
【问题描述】:

我有以下问题:

今天,每个人都想出了一些聪明的短语,让你记住 他们的电话号码。你被赋予了破译这些的任务 短语并找出您需要拨打哪些号码才能联系 这些地方。
说明:您输入的将是一系列字母、数字和破折号。您将需要确定输入序列的编号 以常规的三 - 破折号 - 四格式表示(参见示例输出)。 您还需要确定结果数字是否有效 数字(七位数字),或者如果输入中有数字。
输入:所有字母都将大写。输入字符串最长可达 25 个字符。
输出: 只需一行输出即可打印电话号码,如果号码不是有效号码,或者如果 根本没有数字。
翻译键 ABC = 2 DEF = 3 GHI = 4 JKL = 5 MNO = 6 PRS = 7 TUV = 8 WXY = 9 数字将保持原样并忽略全部 Q, Z 和破折号。示例输入:ITS-EASY 示例输出:487-3279
示例输入:---2---3---TS-4 示例输出:无效数字。
示例输入:QZ---I-M-A-TEST 示例输出:462-8378 示例 输入: ---------- 示例输出:没有电话号码。

我无法将破折号和不必要的字母与翻译成电话号码的实际短语区分开来。到目前为止,这是我的程序:

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 ()
    }

如您所见,我没有取得太大进展。我不知道如何让程序挑选出不必要的字母和破折号,只返回构成数字的字母。有人可以帮我吗?

【问题讨论】:

  • 我是否正确猜测分配不允许用户使用正则表达式和第三方库?

标签: java string loops methods char


【解决方案1】:

要去除字符串中不需要的字符,请查看String.replaceAll

【讨论】:

  • 我不同意这是一个仅链接的答案。这是一个方法调用,将回答 .此方法是关于如何按照 OP 要求去除不需要的字符的完全有效的答案。更多细节会很好,但仍然如此。
【解决方案2】:

这个method will be very handy in your case。多亏了你可以替换它

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

有了这个

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

您也可以通过output.replaceAll( "[^\\d]", "" ) 摆脱所有非数字。最后,您可以在特定位置添加破折号,并检查数字是否有效。

【讨论】:

    【解决方案3】:

    检查以下代码..

    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;
    }
    

    您可以为所有其他数字组合扩展else-if。您不必检查 -QZ 等无效字符。如果输出变量位于if 语句中,它将被编辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 2010-11-30
      • 2023-04-04
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多