+ Start a Discussion
Rohit TripathiRohit Tripathi 

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

Hello,

This is the trailhead questions which I am trying to solve :

Create an Apex class 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. Each string must have a value in the format 'Test n' where n is the index of the current string in the array. The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.


Here 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);
        return List1;
    } 

}


I am getting following 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.


I tried it many times but I am not able to solve this problem. Please help. 



 
Best Answer chosen by Rohit Tripathi
SarfarajSarfaraj
I hope this is your final 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);
        return List1;
    }
}
You may try to copy and past the above to overwrite your existing code.

All Answers

SarfarajSarfaraj
Change this line,
List1.add('\'Test'+ i+'\'' );
To this,
List1.add('Test ' + i);
Rohit TripathiRohit Tripathi
I am still getting following 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.
SarfarajSarfaraj
I hope this is your final 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);
        return List1;
    }
}
You may try to copy and past the above to overwrite your existing code.
This was selected as the best answer
SARANG DESHPANDESARANG DESHPANDE
My code is also the same as Akram gave above. I too,, am getting the same error. Unable to understand what they require exactly...
salesforce instancesalesforce instance
public class StringArrayTest {
 
    public static String[] generateStringArray(Integer length) {
       
        String[] myArray = new List<String>(Integer length)
for(Integer i=0;i<length;i++) {
           
            myArray.add('Test ' + i);

            
            System.debug(myArray[i]);
        } 

        return myArray;
    }   
}

copy the above code.It should work....
Iram MalikIram Malik
Insert space between Test and integer variable.
The code line in the above code should be:
myArray.add('Test' +' '+i);

It should work now.
Steve CurranSteve Curran
I copied the above code and am now receiving an error on line 5; "expecting a right parentheses, found 'length".
Suggestions?
 
Raj I RaoRaj I Rao
Here is the code which was working fine:-----

public class StringArrayTest {
 
    public static String[] generateStringArray(integer len) {
       
        String[] myArray = new List<String>();
            
            for(Integer i=0;i< len;i++) {
                    myArray.add('Test ' +i);          
                    System.debug(myArray[i]);
            } 
            return myArray;
        }   
}
Prudvi SiddharthaPrudvi Siddhartha
Hello Anyone,
I was able to execute the coded successfully provided Rajkumar Rao, but i am not sure where to see the output of the code.
 
satya gadamsettysatya gadamsetty
Prudvi Siddhartha execute your code in anonymous window ,check the open log in it. you'll be able to see logs about your code . If you want to see output of your code check debug only . Hope it helps
SKT CloudComputingSKT CloudComputing
I tried to run code(provided by Rajkumar Rao)  in anonymous window and got belwo error-




Line: 3, Column: 28
Only top-level class methods can be declared static
Ashwini ns 5Ashwini ns 5

In anonymous window type the following to get the output,and then select debug only.

StringArrayTest.generateStringArray( any integer);
for example:
StringArrayTest.generateStringArray(9);
Suresh PalukuruSuresh Palukuru
Can some one confirm whetehr this challenge is working now.. As I'm facing below error even i have done correctly.
Error: Find the screen shot below;
User-added image

My Code
public class StringArrayTest {
     public static List<String> generateStringArray(Integer n) {
           List<String> liststring = new List<String>();
           for (Integer i = 0; i <= n; i++) {
                  liststring.add('Test ' + i);     
           }
          return liststring; 
      }
}
Karthikeya Hebbar 4Karthikeya Hebbar 4
Hi suresh Palukuru,
Just change the "for(Integer i = 0; i <= n; i++)" to "for (Integer i = 0; i <=n; i++)"
Rohit Aryan 6Rohit Aryan 6
yeah karthikeya hebbar is right..
JhydeJhyde
Hi All,
Can someone please let me know what is wrong. I am able to successfully execute the method in the Developer Console. I have an image below, but the challenge in Trailhead is still not accepting my method. I am using a List instead of an Array.
User-added image
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.
Rangeshwara KonaRangeshwara Kona
public class StringArrayTest {
    public static List<String> generateStringArray(Integer n) {
        List<String> string_array = new List<String>();
        for (integer i<=0;i<n;i++) {
            string_array.add('Test ' + i);
        }
            return string_array;
    }
}


Please try above code 
Saikat BanerjeeSaikat Banerjee
public class StringArrayTest {
   // public Integer length=10;
public static list<string> generateStringArray(integer length)
{
    list<string> s=new list<string>();
    for(integer i=0;i<=length;i++){
        string s1='Test' + i;
        s.add(s1);
       system.debug(s[i]);
}
    return s;
}
}

 
Siddig MohamedSiddig Mohamed
thist is working thanks
Vivek NayakVivek Nayak
Hi All,
Seeking for help.

public class StringArrayTest {

   public static list<string> generateStringArray(integer n){
   //n=5;
    list<string> mylist=new list<string>();
    for(integer i=0;i<n;i++){
      string s1='test'+ i;
      mylist.add(s1);
     system.debug(mylist[i]);
    }
   return mylist;     
}
}

Dont know what is wrong with above code.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.

any help would be appreciated.
Thanks
Robi007Robi007
@vivek nayak ..
The issue is with line string s1='test'+ i...  

Because the result must be ('Test 0', 'Test 1', ...). You need to put a space in between :-)
string s1='Test'+ ' ' +  i;
 
Robi007Robi007
@Suresh Palukuru .. I got the same error executing similar code.
But I changed  liststring.add('Test ' + i);   --->  liststring.add('Test' + ' ' + i);  and it all worked :-)

But I dont  know why it didnt work as the code is fine with  liststring.add('Test ' + i);   (a space after Test). It might be with the Saleforce code-checker is coded like that
Mytrail SFDCMytrail SFDC
How to resolve this.

wrote the code , got this msg and i copied from above and still the same
Chalenge 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..... 

Please help
chie lectricchie lectric
Try changing increment on "i".
Use: i=i+1 instead of i++ 
santosh_sahanisantosh_sahani
@Suresh Palukuru : Use @Robi007 soution:
Because the result must be ('Test 0', 'Test 1', ...). You need to put a space in between :-)
string s1='Test'+ ' ' +  i;
It workes for me.
pranav shah 40pranav shah 40
Hi  ,
please help,

i am getting following error in my org 

Error: Compile Error: Variable does not exist: i at line 7 column 16

public class StringArrayTest
{
public static List<String> generateStringArray(Integer n)
{
list<String> a=new List<String>();
for(integer i=0; i<n; i++);
a.add('Test' + i);
return a;
}
}
Su Wang 4Su Wang 4
@pranav shah 40 Change your a.add('Test' + i) to a.add('Test' + ' ' + i)
mani g 23mani g 23
@pranav shah 40 ,remove semicolon at the end of for loop for(integer i=0; i<n; i++)
Shehla Ghori 5Shehla Ghori 5
space after Test is the issue
Ben RowleyBen Rowley
public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n){
        List<String> strings = new List<String>();
        integer count = 0;
        While(count < n){
        strings.add('Test ' + count);
        count ++;
        }
        System.debug(strings);
        return strings;
    }
}
Shoaib SaeedShoaib Saeed
Perfect ! This is working. Thanks Ben.
Ben RowleyBen Rowley
No Problem Shoaib, happy to help! 
diwakar rajputdiwakar rajput
i am unable to execute below code . it gives a error please help me to resolve thisUser-added image
public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n){
        List<String> strings = new List<String>();
        integer count = 0;
        While(count < n){
        strings.add('Test '+count);
        count ++;
        }
        System.debug(strings);
        return strings;
    }
}
Selvakumaran ManiSelvakumaran Mani
Probably, you have missed the semicolon at the end
StringArrayTest.generateStringArray(n);
Ex: StringArrayTest.generateStringArray(2);
yi zhang 43yi zhang 43
my code like this, it works good, but I got error when I want to check challenge:
public class StringArrayTest{
    public static List<String> generateStringArray(Integer n){
        List<String> returnList=new List<String>();
        String value;
        
        if (n<=0){
            value='test'+n;
            returnList.add(value);
        }
        else{
            for (Integer i=0;i<n;i++){
                value='test'+i;
                returnList.add(value);
            }
        }
        System.debug(returnList);
        return returnList;
    }
            
}

Challenge Not yet complete... here's what's wrong: 
No Apex class named 'StringArrayTest' was found
Debapriya KanjilalDebapriya Kanjilal
Hi,

Below is the code I wrote which is not very different from the code given by RajKumar Rao. Can anyone please point me out the defect in my code?

public class StringArrayTest 
{
public static String[] generateStringArray(Integer n)
{
  
    String[] my_list = new List<String>();
    for (integer i=0; i<n; ++i)
    {
        my_list.add('Test'+''+i); //concatenate the iterated i 
        System.debug(my_list[i]);
    }
    return my_list;
}
}
Neha TalyanNeha Talyan
I have written this class 
public class StringArrayTest{

    public static List<String> generateStringArray(Integer n)
    {
        List<String> liststring=new List<String>();
        //n=10;
        for (Integer i=0;i<=n;i++)
        {    
            liststring.add('Test' + ' ' + i);           
        }
        system.debug(liststring);
        return liststring;
    }
}

when i am running this through Developer Console
StringArrayTest.generateStringArray(5);
i am getting below output 
DEBUG|(Test 0, Test 1, Test 2, Test 3, Test 4, Test 5)

But when i am checking chhalenge i am getting below error
Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.
 
Prashant Singh 123Prashant Singh 123
public class StringArrayTest {
    public static list<string> generateStringArray(integer temp)
    {
        list<string> l=new list<string>();
       
      for(integer i=0;i<temp;i++)
      {
         
          l.add('Test '+i);
      }
        return l;
    }

}
 
Priyanshi SuranaPriyanshi Surana
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.

The issue is Post-increment. It should be pre-increment.

Change this line,
 
for(integer i=0;i<n;i++)


To this
 
for(integer i=0;i<n;++i)

 
deepthi namadeepthi nama
public class StringArrayTest {
 
    public static List<String> generateStringArray(integer num){
        List<String> Returnlist = new List<String>();
        For(integer i=0; i<num; i++){
            Returnlist.add('Test '+i);
            System.debug(Returnlist[i]);
           
        }
        Return Returnlist;
         
      
        
    }
}
deepthi namadeepthi nama
public class StringArrayTest {
 
    public static List<String> generateStringArray(integer num){
        List<String> Returnlist = new List<String>();
        For(integer i=0; i<num; i++){
            Returnlist.add('Test '+i);
            System.debug(Returnlist[i]);
           
        }
        Return Returnlist;
         
      
        
    }
}
Joseph Simpson IIJoseph Simpson II
I don't understand why I'm getting the following error (see code below):
Error:
Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.
Code:
public class StringArrayTest{
    
    public static List<String> generateStringArray(Integer arrayLength) {
        List<String> stringArray = new List<String>();
        for(Integer i = 0;i < arrayLength;i++){
            stringArray.add('Test ' + i);
            System.debug(stringArray[i]);
        }
        return stringArray;
    }
}
Neelima SakepallyNeelima Sakepally
Joseph, what does your debug log shows? Please try below code...
 
public class StringArrayTest {
    public static list<string> generateStringArray(Integer numberofStrings){
       List<string> stringlist= new List<string>();
        for(integer i=0;i<numberofStrings;i++)
        {
            stringlist.add('Test '+i);
            system.debug('string added'+i);
        }
       return stringlist; 
    }
    
}

 
Tushar Mehta 9Tushar Mehta 9
you can try below code. Please let me know if it does not work.
I have added debug statements too.

public class StringArrayTest {
        // Public method
        public static string[] generateStringArray (Integer num) {
        List<string> callList = new List<string>();
            for (integer i=0;i<num;i++){
                callList.add('Test'+' '+ i);
                system.debug(callList[i]);
            }
            return callList;
        }
}

Once you add Apex class, you can try below anonymous APEX to execute it.

StringArrayTest.generateStringArray(4);
Sandip KanaSandip Kana
Try this one i wrote:

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;
    }
}
velpula venkateshvelpula venkatesh
Hi all,
its successful,but i found this as Chalenge 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....
JIMI JAINJIMI JAIN
This is working for me.

public class StringArrayTest {
    // Public method
    public static String[] generateStringArray(Integer value) {

        String[] Test1 = new List<String>();
    
    for(Integer i=0;i<value;i++) {
    // Add value to the array
    Test1.add('Test '+i);
            System.debug(Test1[i]);        
                                    }
 
   return Test1; 
                            }
                         }

Please save it and refresh the trail and then try to complete the Challenge.
Stephen CoutinhoStephen Coutinho
Still getting the message:
<Challenge not yet complete in My Trailhead Playground 1
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 void generateArray(Integer num1){
        String[] numList1 = New List<String>();
        numList1 = generateStringArray(num1);
    }
    
    public static String[] generateStringArray(Integer num2){
        String[] numList = New List<String>(num2);
        
        for(Integer i = 0; i<num2; i++){
            numList[i] = 'List ' +i;
            system.debug(numList.get(i));
        }
        system.debug(numList);
        return numList;
    }
    
}

User-added image
Stephen CoutinhoStephen Coutinho
My mistake, it should have been 'Test' instead of 'List'
Meghraj KurmiMeghraj Kurmi
All the above codes and logics are correct if still, you are getting an error, jus you need to refresh your trailhead.
Martin MacangaMartin Macanga
one of the problem also is:
for everyone who has = in statement for(Integer i=0;i<=length;i++) it doesnt work ! 
try without = and its done :)
like this: for(Integer i=0;i<length;i++)  and dont forget has others things also right ... 
suman balususuman balusu
@Stephen Coutinho thanks bro ..it worked out 
Uttam BeheraUttam Behera
public class StringArrayTest {
    public static List <String> generateStringArray(Integer n){
        List<String> myList = new List<String>();
           //loop 
        for(Integer i=0; i<n; i++) {
            // Add the value into the list
            myList.add('Test' + ' ' + i);
        }
        
        System.debug(myList);
        return myList;
    }
}


 
Mark LaingMark Laing
My code works with a test but when I go to check this challenge I get the not complete error and I get the below in the dev console logs.  Any ideas? 
I have even created a new class and tried code from this page but I get the exact same error in the logs.

FATAL_ERROR System.AssertException: Assertion Failed: Expected: Test0, Actual: Test 0

The current code is from this page.

public class StringArrayTest 
{
    public static String[] generateStringArray(Integer n)
    {
        String[] List1 = new List<String>();
        integer count = 0;
        While(count < n)
        {
            List1.add('Test' + count);
            count ++;
        }
        System.debug(List1);
        return List1;
    }    
     
}
 
Uttam BeheraUttam Behera
I have tested following code  and it work fine.
public class StringArrayTest {
    public static List <String> generateStringArray(Integer n){
        List<String> myList = new List<String>();
           //loop 
        for(Integer i=0; i<n; i++) {
            // Add the value into the list
            myList.add('Test' + ' ' + i);
        }
        
        System.debug(myList);
        return myList;
    }
}

 
Mark LaingMark Laing
@Uttam Behera
Exactly as my original code was but I didn't have a space between the two apostrophes in
myList.add('Test' + ' ' + i);

Took re-reading your code to see it hahaha, thanks!!!  I am such a noob at this! ;)
Vani N 15Vani N 15
yes!! after saving this code and execution works for mee and challenge completed