0

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.

7
  • 3
    Possible duplicate of Java: Finding the shortest word in a string and printing it out Commented Mar 2, 2018 at 15:01
  • 1
    try to print out what does setWordArray returns when you send "hello you" with a special char (just to verify it returns a correct thing) Commented Mar 2, 2018 at 15:03
  • 2
    And no this is not a duplicate. (he has a problem with the regex it seems, it's not about "how to find the shortest word" but "what is wrong in my method", which is a different thing. Commented Mar 2, 2018 at 15:05
  • "Anaylsed" uesd at laest eigth tiems? Teh IDE seems to hlep yuo in teh worng way... Maybe it's also a sign that you shouldn't use such monstrously long identifiers for local variables in such a short and simple method, but rather move the description into the @param section of JavaDoc instead. Commented Mar 2, 2018 at 15:08
  • @Henleyn I have it prints out an empty line then Hello you Commented Mar 2, 2018 at 15:16

1 Answer 1

2

Answering to your comments: When you have a space in the beginning, split will have one more element, an empty String, in the beginning. That one is the shortest element in the array (check for the array's length), but printing it reveals nothing that's visible. But, rest assured, the correct element is printed: it's ""

You might want to trim your String before the split operation

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the feedback. Note: 1) That's also what the "accept" button is for 2) it always worked: Just that the shortest word was so short, it printed nothing (because the empty string is shorter than all others) and was hard to identify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.