【问题标题】:Trouble Converting a String to Acronyms将字符串转换为首字母缩写词时遇到问题
【发布时间】:2014-11-19 18:55:25
【问题描述】:

我正在研究一种方法,该方法将字符串数组作为输入并返回其首字母缩略词的数组,这些首字母缩写词仅为大写字母。

例如:

[United Nations,  United Federation of Planets, null, , ignore me] -> [UN, UFP, null, , ]

由于某种原因,我的代码没有返回任何内容,而且它还显示 null 检查是无效代码,我不知道为什么。

public static String[] convertStringsToAcronyms(String[] input) 
{
    int itemCount = input.length;
    String[] result = new String[itemCount];
    int k = 0;
    for (String words : input) {
        boolean checklowercase = words.toLowerCase().equals(words);

        if (checklowercase || (words == ""))
            result[k] = "";

        if (words == null)
            result[k] = null;

        String add = "";

        String[] ary = words.split("");
        for (String letter : ary) {
            char firstletter = letter.charAt(0);
            if (Character.isUpperCase(firstletter))
                add = add + firstletter;
        }

        result[k] = add;
        k++;
        add = "";

    }
    return result;

}

【问题讨论】:

  • 不要太挑剔,但是一旦你有了一个工作方法,你最好让这个方法接受并返回一个字符串,而不是一个数组。这概括了一些。让调用方法操心数组的处理,然后你就可以从只有一个字符串的方法中调用它。

标签: java arrays string dead-code acronym


【解决方案1】:

空值检查是死代码,因为在它之前你访问了words 变量,所以如果它为空值,你会在空值检查之前得到一个NullPointerException

boolean checklowercase = words.toLowerCase().equals(words);
....
if (words == null) // this can never be true
    result[k] = null; // this is dead code

【讨论】:

    【解决方案2】:

    我认为这会更优雅地完成你所追求的。

    public static sampleAcronymMethod()
    {
        String[] arr =  {"United Nations",  "United Federation of Planets"};
        for (String element : arr) //go through all of our entries we wish to generate acronyms for
        {
        String[] splits =   element.split("[a-z]+");//remove all lowercase letters via regular expression
        String acronym = "";//start with an empty acronym
    
        for (String split : splits)//go through our uppercase letters for our current array entry in arr 
            acronym = acronym + split;//tack them together
    
        acronym = acronym.replaceAll("\\s","");//remove whitespace
    
       System.out.println("Acronym for " + element + " is " + acronym);
       }
    }
    

    【讨论】:

    • 我想在一个字符串数组中,所以我尝试将首字母缩写词转换为一个数组并返回它,但是我得到一个错误,这个变量没有定义!
    • 创建另一个数组并在每次迭代后添加到它:)
    【解决方案3】:

    在 Java 1.8 中更加优雅

        String[] i = new String[] {"United Nations",  "United Federation of Planets", null, "", "ignore me"};
    
        String[] array = Arrays.stream(i)
                .filter(it -> it != null && !it.isEmpty())
                .map(it -> it.split(" "))
                .map(words -> Arrays.stream(words)
                        .map(w -> w.substring(0, 1))
                        .filter(l -> l.toUpperCase().equals(l))
                        .collect(Collectors.joining()))
                .toArray(String[]::new);
    
    
        System.out.println(Arrays.toString(array));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多