使用字符串生成器和拆分更改 java 电话格式

标签 java arrays string builder

我尝试将字符串电话号码重新格式化为 +1(204)867-5309,首先将中间的点分开,然后检查输入是否有效,如果不是 抛出新的IllegalArgumentException。但由于某种原因,即使我使用字符串生成器来更改数字的输出,它也不会改变。 提前致谢。

public class format{
    public static void main (String[] args){
        String phone = "204.867.5309"; 
        System.out.println(format(phone));
    }

    public static String format(String phone){
        char [] phoneLine = phone.toCharArray(); 
        String [] split = phone.split("\\."); 

        for (char c: phoneLine){
            if (phoneLine[3]=='.'
            && phoneLine[7]=='.'
            && phoneLine.length == 12 
            && Character.isDigit(phoneLine[1])
            && Character.isDigit(phoneLine[0])
            && Character.isDigit(phoneLine[2])
            && Character.isDigit(phoneLine[4])
            && Character.isDigit(phoneLine[5])
            && Character.isDigit(phoneLine[6])
            && Character.isDigit(phoneLine[8])
            && Character.isDigit(phoneLine[9]))
            {  
                StringBuilder sb = new StringBuilder(phone); 
                sb.append("+1");
                sb.insert(2,"("); 
                sb.insert(6, ")");
                sb.insert(10,"-");
            }
            else {
                throw new IllegalArgumentException("not valid"); 
            }
        }
        return phone; 
    }
}

最佳答案

你可以尝试下面的代码.output:20(48)67-5309+1

public class PhoneNumberFormat {
public static void main(String[] args) {
    String phone = "204.867.5309";
    System.out.println(format(phone));
}

public static String format(String phone) {
    String[] split = phone.split(".");
    split = phone.split("\\.");
    String newPhone = "";
    for (String s : split) {
        newPhone = newPhone + s;

    }
    char[] phoneLine = newPhone.toCharArray();
    StringBuilder sb = new StringBuilder();
    for (char c : phoneLine) {
        if (phoneLine.length == 10
                && Character.isDigit(phoneLine[1])
                && Character.isDigit(phoneLine[0])
                && Character.isDigit(phoneLine[2])
                && Character.isDigit(phoneLine[4])
                && Character.isDigit(phoneLine[5])
                && Character.isDigit(phoneLine[6])
                && Character.isDigit(phoneLine[8])
                && Character.isDigit(phoneLine[9])) {
            sb = new StringBuilder(newPhone);
            sb.append("+1");
            sb.insert(2, "(");
            sb.insert(6, ")");
            sb.insert(10, "-");
        } else {
            throw new IllegalArgumentException("not valid");
        }
    }
    return sb.toString();
}}

关于使用字符串生成器和拆分更改 java 电话格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59997802/

相关文章:

java - Spring + Hibernate表未生成

java - 在 JBoss EAP 5.1 中为特定的 MDB 设置 maxSession 属性

c - 学习c编程——将数组传递给函数

javascript - 返回包含字符串的嵌套数组的最有效方法 (JavaScript)

java - 尝试设置数组元素约束

string - 搜索字符串中的某些单词或短语

Python:检查字符串是否在列表中的任何项目中?

java - 使用 Twitter-hbc 在 Java 中读取推文 - IOException

java - 线程运行如何开始?

asp.net - udl 连接字符串有效,但我的 ASP.Net 文件在连接时抛出异常