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
raushan kumar 34raushan kumar 34 

Create an Apex class that returns an array (list) of strings

getting error: 
Challenge Not yet complete... here's what's wrong: 
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 string[] generateStringArray(integer n){
        
        List<string> l1=new LIst<String>(n);
        
        for(integer j=0; j< n; j++){
            l1.add(j, 'Test '+j);
        }
        return (l1);
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000BTQ9IAO
 
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;
    }
}
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);

Please let us know if this will help you


 
maurya sekhar 4maurya sekhar 4
public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n ){
        // creating a new empty list
        List<String> testStrings = new List<String>();
        //create a for loop to add strings to the list taking size from the parameter
        for(Integer i  = 0 ; i < n ; i++ ){
            System.debug('i= '+ i);
            testStrings.add('Test' + i);
            
        }
        System.debug(testStrings);
        return testStrings;
        
    }

}

Ctrl + E opens Anonymous Window 
StringArrayTest.generateStringArray(5);

// the above mentioned issue is because of  space between test and 0 use ('Test ' + 0)
phanidevphanidev
This is working!


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



Execute Anonymous Window 

list<String> listvariable = new list<string>();
listvariable = StringArrayTest.generateStringArray(5);
system.debug('**********' +listvariable);