You need to sign in to do that
Don't have an account?
Chaitra GV
Trialhead challenge error :Executing the 'generateStringArray' method failed
Hi, I'm facing below error on trialhead challenge even though the code is correct. Got expected results in the debug window as well.
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.
Code:
public class StringArrayTest
{
public static List<String> generateStringArray(Integer n)
{
String[] arr = new List<String>();
for(Integer i=0;i<n;i++)
{
arr.add('Test'+i);
}
System.debug('The array is:'+arr);
return arr;
}
}
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.
Code:
public class StringArrayTest
{
public static List<String> generateStringArray(Integer n)
{
String[] arr = new List<String>();
for(Integer i=0;i<n;i++)
{
arr.add('Test'+i);
}
System.debug('The array is:'+arr);
return arr;
}
}
The problem in your code is that you need to pass one space between 'Test '+i .
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;
}
}
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi
All Answers
Try the below code:
public class StringArrayTest {
public static List<String> generateStringArray(Integer n)
{
List<String> arr = new List<String>();
for(Integer i=0;i<n;i++)
{
arr.add('Test '+i);
System.debug(arr[i]);
}
return arr;
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thank,
Raj
The problem in your code is that you need to pass one space between 'Test '+i .
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;
}
}
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi
Hi Chaitra GV,
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,
Please make it best if it helps you.
Thanks
Akshay