0
      import java.util.*;
      public class Solution {
      
        public static void main(String[] args) {
          int secLen = 2;
          solve(secLen);
        }
        public static String solve(int sequenceLength) {
          int sec = sequenceLength+1;
          String[] fibSequence = new String[sec];
          fibSequence[0] = "0";
          *fibSequence[1] = "01";*
          
      
          for(int i = 2; i <= sequenceLength;i++){
            fibSequence[i] = fibSequence[i-1] + fibSequence[i-2];
          }
           
          
          
          System.out.println(fibSequence[sequenceLength]);
          //return fibArray;
          return fibSequence[sequenceLength];
        }
        
      } 

CodeWars is telling me there is an index out of bound error in line 15. It makes no sense though because when I test it in my IDE everything perfectly works.

  • 1
    Please mark line 15 with a comment. Also what is the input which causes the error ? What is the desired output ? This one only prints series of 001 – c0der Nov 25 '21 at 09:18
  • 1
    you don't handle the special cases of an empty array, or the one of size 1 – Lino Nov 25 '21 at 09:25
  • Indeed - if `sequenceLength` is 0, you'll create a single-element array, then try to populate two elements in it... – Jon Skeet Nov 25 '21 at 09:27
  • I have marked it with * – Justlearning Nov 25 '21 at 09:37
  • Line15: ```fibSequence[1] = "01";``` caused an ```ArrayIndexOutOfBoundsException``` **=>** This means that the length of the array is 1 **=>** When will the length of the array be 1? **=>** ```sec=1``` **=>** ```sequenceLength=0```=> Add a check:```if (sequenceLength == 0) return "xxxx";``` – zysaaa Nov 25 '21 at 10:13
  • Hint: nothing in your code example relies on data passed into it. Just run your code locally, and ask yourself: what expectations do I have about my variables, and what is ACTUALLY there?! – GhostCat Nov 25 '21 at 10:32

1 Answers1

0

Why is your FibSequence array of type string. I'm assuming you would want to calculate the sum of the values. Make it of type int because Strings are immutable. Also handle cases where the input is null or a negative number.

Zaid Fanek
  • 23
  • 5
  • No, the task was to exactly do that with combining the number Strings. The problem is that everything works fine in my IDE and everything should work. – Justlearning Nov 25 '21 at 09:41