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
Reshma SWAMY 2Reshma SWAMY 2 

I am getting 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 me to solve this

this is my code.

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

}
Best Answer chosen by Reshma SWAMY 2
SandhyaSandhya (Salesforce Developers) 
Hi,

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

https://developer.salesforce.com/forums/?id=9060G000000I2bhQAC
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

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

https://developer.salesforce.com/forums/?id=9060G000000I2bhQAC
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
 
This was selected as the best answer
Kapil GugoriaKapil Gugoria

Hi All,

I am trying to complete the below trailhead challenge 
===========================================================================================
Challenge of Trail head 

Create an Apex class with a method that returns an array (or list) of strings.
Create an Apex class with a method that returns an array (or list) of formatted strings ('Test 0', 'Test 1', ...). The length of the array is determined by an integer parameter.
The Apex class must be called StringArrayTest and be in the public scope
The Apex class must have a public static method called generateStringArray
The generateStringArray method must return an array (or list) of strings
The method must accept an incoming Integer as a parameter, which will be used to determine the number of returned strings
The method must return a string value in the format Test n where n is the index of the current string in the array
=============================================================================================
Below is the code 

public class StringArrayTest {
    
    public static List<string> generateStringArray(Integer length)
    {
        List<string> mydata = new List<String>();
        for (Integer i=0;i<length;i++)
        {
            mydata.add('Test' + i);
        }
        system.debug(mydata);
        return mydata;
    }
    
}
=============================================================================================

   Below is the result 

18:10:45:012 USER_DEBUG [10]|DEBUG|(Test0, Test1, Test2, Test3)
=============================================================================================

but the issue is while completing my challenge  trailhead is giving the error which is ....

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

Madhu Reddy 110Madhu Reddy 110
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;
    }    
}

 
Madhu Reddy 110Madhu Reddy 110
Use this code.  I tried wirth it. Its working for me