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
Akanksha BhardwajAkanksha Bhardwaj 

Receiving Error:"Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings" when trying to complete "Getting started with Apex" challenge

Here is my code: 
public class StringArrayTest {
    public static List<String> generateStringArray(Integer len){
        List<String> myarr = new List<String>();
        for(integer n=0;n<=len;n++)
        {
            myarr.add('Test' +n);
            system.debug(myarr[n]);
        }
        return (myarr);
    }
}
Best Answer chosen by Akanksha Bhardwaj
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000BTQ9IAO


Problem in your code is that you need to pass one space between 'Test '+i and you need to start loop from zero

Please try below code:-
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;
    }     
}
And go :
Your Name>Developer Console>Debug>Open Execute Anonymous Window 
Paste below code in it
list<string> myArray = StringArrayTest.generateStringArray(10);
system.debug('************'+myArray);


The Apex class must be called 'StringArrayTest' and be in the public scope.
--> public class StringArrayTest{
The Apex class must have a public static method called 'generateStringArray'.
-->public static List<String> generateStringArray(Integer n) {

The 'generateStringArray' method must return an array (or list) of strings.
-->public static List<String> generateStringArray(Integer n) {

Each string must have a value in the format 'Test n' where n is the index of the current string in the array.
   s= 'Test ' + x;

The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.
  public static List<String> generateStringArray(Integer n) {


Let us know if this will help you

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Akanksha,

Please check with below links from forums community with the same challenge and suggested workarounds. Please mark this as solved if this helps.

Regards,
Nagendra.
SandhyaSandhya (Salesforce Developers) 
Hi,

Please refer below code that worked for me.
 
public class StringArrayTest 
{
    //Public Method
    public static List<string> generateStringArray(Integer n)
    {
        List<string> StringArray = new List<string>();
        for(Integer i=0; i<n; i++)
        {
            StringArray.add('Test ' +i);
            System.debug (StringArray[i]);
        }
            return StringArray;  
     }
}



Hope this helps you!

Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya


 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000BTQ9IAO


Problem in your code is that you need to pass one space between 'Test '+i and you need to start loop from zero

Please try below code:-
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;
    }     
}
And go :
Your Name>Developer Console>Debug>Open Execute Anonymous Window 
Paste below code in it
list<string> myArray = StringArrayTest.generateStringArray(10);
system.debug('************'+myArray);


The Apex class must be called 'StringArrayTest' and be in the public scope.
--> public class StringArrayTest{
The Apex class must have a public static method called 'generateStringArray'.
-->public static List<String> generateStringArray(Integer n) {

The 'generateStringArray' method must return an array (or list) of strings.
-->public static List<String> generateStringArray(Integer n) {

Each string must have a value in the format 'Test n' where n is the index of the current string in the array.
   s= 'Test ' + x;

The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.
  public static List<String> generateStringArray(Integer n) {


Let us know if this will help you
This was selected as the best answer
Hanuma RudrarajuHanuma Rudraraju
Receiving Error:"Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings" when trying to complete "Getting started with Apex" challenge

I got this error
But when I replaced the list with array syntax then it worked
PRASHANT SONTAKKEPRASHANT SONTAKKE
even i am too facing the same problem
 
Areeb Panjwani 4Areeb Panjwani 4
The following worked for me. (Dont forget to add the space after test)


public class StringArrayTest {

    public static List<String> generateStringArray(Integer i ){

        List<String> LS = new List<String>();

        for(Integer n = 0; n<i; n++){
            LS.add('Test'+ ' ' + n);
            
            
        }
        system.debug(LS);
        return LS;
        
    }

}
Shivanee Hingwe 6Shivanee Hingwe 6
I was getting the same code .Here is the code that worked for me -
public class StringArrayTest {
public static List<String> generateStringArray(Integer size)
{
    List<String> Test = new List<String>();
    for(Integer i = 0 ; i< size;i++)
    {
        Test.add('Test '+String.valueOf(i));
        System.debug(Test[i]);
    }
    return Test;
}
}

​​​​​​​
Freddy SmithFreddy Smith
Note: Check in Developer Console Logs tab after you have tried the "Challenge" button in Trailhead module as you get some errors saying things like "Assertion Failed: Expected 11, Actual: 10" so the trailhead code challenge supplies a few tips in the logs!

The below worked for me after I got some ideas from others here thanks!

public class StringArrayTest {
   
// Public method
    public static String[] generateStringArray(Integer size) {
       
// Create a list and add elements to it in a for loop
        String[] myArray = new List<String>();
      
 // Iterate to create array of 'size' variable
                for(Integer i=0;i<size;i++) {
                   
// add values to myArray
                    myArray.add('Test '+ i);
                    System.debug('Array value ' + i + ' : ' + myArray);           
                }
        System.debug('RETURNED Array value : ' + myArray);           
        Return myArray;                  
     }                                 
}



I used the below code to start my code from Developer Console select Debug tab - Open Execute Anonymous window. You can also Ctrl-E to open the execute anonymous window.

StringArrayTest.generateStringArray(10);

Thanks