You need to sign in to do that
Don't have an account?
Akanksha 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);
}
}
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);
}
}
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:- And go :
Your Name>Developer Console>Debug>Open Execute Anonymous Window
Paste below code in it
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
Please check with below links from forums community with the same challenge and suggested workarounds.
- https://developer.salesforce.com/forums/?id=906F0000000MJbnIAG
- https://developer.salesforce.com/forums/?id=906F0000000BSlVIAW
- https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AvPBIA0
Please mark this as solved if this helps.Regards,
Nagendra.
Please refer below code that worked for me.
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
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:- And go :
Your Name>Developer Console>Debug>Open Execute Anonymous Window
Paste below code in it
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
I got this error
But when I replaced the list with array syntax then it worked
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;
}
}
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