I am trying to find the shortest word from a string array, but I am removing special characters using reg ex with the code
public String[] setWordArray(String stringToBeAnaylsedForFrequency) {
stringToBeAnaylsedForFrequency = stringToBeAnaylsedForFrequency.replaceAll("\\d+", " ");
stringToBeAnaylsedForFrequency = stringToBeAnaylsedForFrequency.replaceAll("\\W", " ");
stringToBeAnaylsedForFrequency = stringToBeAnaylsedForFrequency.replaceAll("( )+ ", " ");
String[] wordArray = stringToBeAnaylsedForFrequency.split(" ");
return wordArray;
}
and this is the method for use
public String getShortestWordInStringGiven() {
int wordArrayLength = getStringArrayForGivenString().length;
String shortestWordInGivenString = getStringArrayForGivenString()[0];
for (int i = 1; i < wordArrayLength ; i++) {
if (getStringArrayForGivenString()[i].length() < shortestWordInGivenString.length()) {
shortestWordInGivenString = getStringArrayForGivenString()[i];
}
}
return shortestWordInGivenString;
}
when it works fine i input text like hello you, it would return you as the shortest character, but when i input "hello you" with a special character at the start it returns nothing.
@paramsection of JavaDoc instead.