-1

When I submit my code to Leetcode, it reported runtime error as:

Runtime Error Message: Line 8: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 

I tested that case in my local, it works fine. I thought it maybe causeed by the platform and compiler are different. I then tried to test it on Leetcode Playground. It also worked very well. The Leetcode problem is:https://leetcode.com/problems/string-to-integer-atoi/ I would be very appreciated if anyone could let me know what's wrong with my code.

class Solution{
public int myAtoi(String str) {
        if (str == null || str.length() == 0) return 0;
        char chs[] = str.toCharArray();
        long base = 0;
        int i = 0, sign = 1;

        while (chs[i] == ' ' && i < str.length()){
                i++;
        }
        if(i == str.length()){
            return 0;
        }
        if (chs[i] == '-') {        
            i++;
            sign = -1; 
        } else if (chs[i] == '+') { 
            i++;
        }
        while (i < str.length() && (chs[i] >= '0' && chs[i] <= '9')) {
            base = base * 10 + (chs[i] - '0');
            if (sign * base > Integer.MAX_VALUE) return Integer.MAX_VALUE;
            if (sign * base < Integer.MIN_VALUE) return Integer.MIN_VALUE;
            i++;
        }
        return (int)(sign * base);
    }
}

enter image description here

Harsh
  • 350
  • 1
  • 4
  • 13
Jessie
  • 1,155
  • 3
  • 16
  • 27
  • You are trying to trim the spaces initially . Can we try doing it using String trim() method rather than checking for (if chs[i] == ' ')? Just a suggestion. Why I am suggesting this is the error is on that line only . – Harmandeep Singh Kalsi May 24 '20 at 03:42
  • The code you've shown in a screenshot doesn't match the code you've provided as text. Please provide a [mcve] that actually reflects the code that's failing. – Jon Skeet May 24 '20 at 06:01
  • 1
    Hint: if `i` is greater than or equal to `str.length()` then `chs[i] == ' ' && i < str.length()` is still going to throw an exception, because it evaluates the left hand side first. Reverse that to `i < str.length() && chs[i] == ' '` and it won't... – Jon Skeet May 24 '20 at 06:50

1 Answers1

1

If pass empty string (one space or more) to myAtoi(" ") in while statement you will go beyond the boundaries of the array:

// chs = {' '}; chs.length = 1; i = 0;
while (chs[i] == ' ') {
      i++;
}

You can add an additional condition i < chs.length to while loop:

while (i < chs.length && chs[i] == ' ')

screenshot with result

Community
  • 1
  • 1
Ex3mS1ze
  • 11
  • 1
  • 3
  • I've already tried to add this i < chs.length() in my while loop before. Still pops up the same error. – Jessie May 24 '20 at 04:56
  • Maybe I'm missing something, but I tried using your code with this change. Exception didn't appear. Added a screenshot with the result in answer. – Ex3mS1ze May 24 '20 at 05:17
  • 1
    @Jessie: You've shown an example as an image which *isn't* the same as this - it's got the same conditions, but they're evaluated in the opposite order. – Jon Skeet May 24 '20 at 06:02