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
Chaitra GVChaitra GV 

Trialhead challenge error :Executing the 'generateStringArray' method failed

Hi, I'm facing below error on trialhead challenge even though the code is correct. Got expected results in the debug window as well.

Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.

Please help.

Code:

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

 
Best Answer chosen by Chaitra GV
Ajay K DubediAjay K Dubedi
Hi Chaitra,

The problem in your code is that you need to pass one space between 'Test '+i .

public class StringArrayTest {
        public static String[] generateStringArray(Integer length) {
        String[] myArray = new List<String>();
        for(Integer i=0;i<length;i++) {
           myArray.add('Test ' + i);

            System.debug(myArray[i]);
        } 
        return myArray;
    }     
}

Please mark it as best Answer if you find it helpful.


Thank You
Ajay Dubedi

All Answers

Maharajan CMaharajan C
Hi Chaltra,

Try the below code:

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

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thank,
Raj
Raj VakatiRaj Vakati
Uss this code
 
public class StringArrayTest {
    public Static List<String> generateStringArray(Integer input){
      	List<String> test = new List<String>();
        for(Integer i=0;i<input;i++){
            test.add('Test '+i);
        }
        return test;
    }
}

 
Ajay K DubediAjay K Dubedi
Hi Chaitra,

The problem in your code is that you need to pass one space between 'Test '+i .

public class StringArrayTest {
        public static String[] generateStringArray(Integer length) {
        String[] myArray = new List<String>();
        for(Integer i=0;i<length;i++) {
           myArray.add('Test ' + i);

            System.debug(myArray[i]);
        } 
        return myArray;
    }     
}

Please mark it as best Answer if you find it helpful.


Thank You
Ajay Dubedi
This was selected as the best answer
Akshay_DhimanAkshay_Dhiman

Hi Chaitra GV,

Please find below a little fix for your issue.

The problem was the space between Test and the number you had: Test0, Test1.... and what was necessary was Test 0, Test 1, 
 

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;
    }
}
 


Please make it best if it helps you.

Thanks

Akshay

Chaitra GVChaitra GV
Thank you all. The issue is resolved now