+ Start a Discussion
BijaySBijayS 

Getting Error While taking the challenge for Getting Started with Apex

User-added image

My Apex Class looks like this :

public class StringArrayTest {
    public static List<String> generateStringArray(Integer n) {
    
        List<String> generateString = new List<String>();
        
        for(Integer i = 0; i <= n ; i++) {
            string formattedString = 'Test ' + i;
            generateString.add(formattedString);
        }
        System.debug(LoggingLevel.INFO, 'Formatted String for Trail Head'+generateString);
        return generateString;
    }
}

Not Sure whats wrong here that i am getting the error.
Amit Chaudhary 8Amit Chaudhary 8
I hope your this issue is resloved in below post.
https://developer.salesforce.com/forums/ForumsMain?id=906F00000005KAQIA2
 
public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n)
    {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)
        {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}
Simply put, your array is returning 1 more record than it should be, because your loop operates over i <= num instead of just i < num.

Please mark this as resolved if your issue resolved