function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Fayazuddin MohammedFayazuddin Mohammed 

StringArrayTest

Hello, Can anyone please tell me what's wrong with my code. When I run it executes fine and o/p also seems to be correct. Howeve the trailhead challenge shows error "Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings."

public class StringArrayTest {
    public static List <String> generateStringArray(Integer n) {
        List<String> numbers = new List<String>();
        
        for (integer i=0;i<=n; i++){
              numbers.add('Test '+String.valueOf(i));
            System.debug(numbers[i]);            
                }
        return numbers;
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Your For loop should be like below

for(Integer i=0;i<n;i++)

NOTE :- It shoud be < n not <=n

 
public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n)
    {
        List<String> numbers = new List<String>();
        for(Integer i=0;i<n;i++)
        {
           numbers.add('Test '+i);
           System.debug(numbers[i]);
        }
        return numbers ;
    }
}


PLease check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000BWSOIA4
2) https://developer.salesforce.com/forums/?id=906F0000000BTQ9IAO

Let us know if this will help you

 
Fayazuddin MohammedFayazuddin Mohammed
Thanks for your help, Amit !  I think the issue is not with i<n or I<=n, since the length of array determined by integer parameter that was given in the challenge. For example if you had output like (Test 0, Test 1 Test 2) OR (Test 0, Test 1, Test 2, Test 3) for given input generateStringArray(3), the test should still pass as long as the o/p is in format (Test 0, Test 1 ....Test n) .

I was using only this line  StringArrayTest.generateStringArray(5);  in my
Execute anonymous window. I used below 2 lines after checking the links you have posted. It works fine.

list<string> myArray = StringArrayTest.generateStringArray(5);
system.debug(+myArray); // This line is important I think as it gives the output in our desired format.