You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
https://developer.salesforce.com/forums/?id=906F0000000AvPBIA0
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, ... And go :
Your Name>Developer Console>Debug>Open Execute Anonymous Window
Paste below code in it
Please let us know if this will help you
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)
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);