I'm working on a method which gets as an input an array of Strings and returns an array of its Acronyms that are only capital letters.
For example:
[United Nations, United Federation of Planets, null, , ignore me] -> [UN, UFP, null, , ]
For some reason my code does not return anything, and it also shows me that the null check is a dead code and I can't figure out why.
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;
}
The null check is dead code because prior to it you access the
words
variable, so if it's null, you'll get aNullPointerException
before the null check.