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
Jesus GJesus G 

Create an Apex class that returns a list of strings

Hello!

As part of the Challenage proposed for the 'Getting Started with Apex' trailhead topic, I am tryting the following:
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.

If I create a class just with the first 3 lines of the following code and execute the rest in an anonymous windows, it works as expected. However, could I have everything just in one class?

public class StringArrayTest {
    public static Integer generateStringArray = 10;
}
        for(Integer i=0;i<StringArrayTest.generateStringArray;i++) {
            System.debug('Test '+ i);
        }

Many thanks!
Jesus
Best Answer chosen by Jesus G
Jesus GJesus G
Thank you very much for your replies!

I already found a post with the same question (https://developer.salesforce.com/forums/?id=906F0000000AvPBIA0) where similar code to yours was provided and it worked perfectly.

Many thanks for your help :)
Jesus

All Answers

sharathchandra thukkanisharathchandra thukkani
Yes you can have all the code in one class but you need to change the code as below.

public class StringArrayTest {
    public static Integer generateStringArray = 10;

   public static void myMethod(){
        for(Integer i=0;i<generateStringArray;i++) {
            System.debug('Test '+ i);
        }
    }
}

you can call myMethod() from anonymous window as StringArrayTest.myMethod();
      
Jesus GJesus G
Hello Sharathchandra,

Many thanks for your reply!

That worked but I am still getting this error message when checking the challenge:

Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.

Any idea?

Thank you very much,
J
sharathchandra thukkanisharathchandra thukkani

try this.

public class StringArrayTest {
    public static Integer numberOfStrings = 10;

   public static List<String> generateStringArray(){
       List<String> returnList = new List<String>();
        for(Integer i=0;i<numberOfStrings ;i++) {
           returnList.add('Test '+ i);
        }
      return returnList;
    }
}

Amit Chaudhary 8Amit Chaudhary 8
Hi Jesus G,

As per task. please change your methed name to "generateStringArray"
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;
    }
}
Please let us know if this will help you
 
Jesus GJesus G
Thank you very much for your replies!

I already found a post with the same question (https://developer.salesforce.com/forums/?id=906F0000000AvPBIA0) where similar code to yours was provided and it worked perfectly.

Many thanks for your help :)
Jesus
This was selected as the best answer
Deepika Reddy 16Deepika Reddy 16
The following code doesnt work for me.....

CODE:
public class StringArrayTest 
{
    public static list<string> generateStringArray(integer n)
    {
           list<string> t =new list<string>();
           for(integer i =0;i<n; i++)
           {
               t.add('Test'+ i);
               
            }
             return t;     
    }

}

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.
Amit Chaudhary 8Amit Chaudhary 8
Please add space after test like below
t.add('Test '+ i);
Let us know if this will help you
Deepika Reddy 16Deepika Reddy 16
how to practise triggers complete code 
Prerana ShikharePrerana Shikhare
Try this code:

public class StringArrayTest {
    public static List <String>  generateStringArray(Integer n){
        
        List <String> testArray= new List<String>();
        
        
        for (Integer i=0;i<n;i++)
        {
           testArray.add('Test '+i);//add space after Test
               
        }
     return(testArray)  ; 
    }
}

If above works for you please mark it as correct/like.
Maria MachadoMaria Machado
How can I use the anonymous woindow to test this code?
 
Amit Chaudhary 8Amit Chaudhary 8
yes try below code to execute in anonymous window
list<string> myArray = StringArrayTest.generateStringArray(10);
system.debug('************'+myArray);

 
Janice Matthies 7Janice Matthies 7
Thanks @Amit Chaudry 8.