wired run time err


  • 0
    D

    Why I got Array index out of bounds at line 9 for input " "(A whitespace)?
    Thanks in advanced!
    Here is my code:

    public class Solution {
    public String reverseWords(String s) {

        if(s == " "|| s == null){
            return s;
        }
        
        String[] wordsArray = s.split(" ");
        if(wordsArray[0] == " "){    //Runtime Error Message: Line 9: java.lang.ArrayIndexOutOfBoundsException: 0
            wordsArray[0] = "";
        }
        
        StringBuilder result = new StringBuilder();
        for (int i = wordsArray.length - 1; i > -1; i--) {
          result.append(wordsArray[i]);
          result.append(" ");
        }
        return result.toString();
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.