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
Chris MarzellaChris Marzella 

Getting Started with Apex Challenge

I'm not sure where I am going wrong. Any help would be appreciated.

Here is my code:
public class StringArrayTest {
    public static String[]generateStringArray(){
        List<String> Test = new List<String>{'Test 0', 'Test 1', 'Test 2'};
            
            for(Integer n=0; n<Test.size(); n++){
                System.debug('Test' + n);
            }
        return Test;
    }
}
This is the 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.
 
Best Answer chosen by Chris Marzella
William TranWilliam Tran
You need to pass in a parameter x
and make sure Test has a space in it like 'Test ' + n

Try this:

thx.
 
public class StringArrayTest {
    public static String[]generateStringArray(Integer x){
        List<String> Test = new List<String>(); 
            for(Integer n=0; n<x; n++){
            Test.add('Test ' + n);}  
            }
        return (Test);
    }
}

All Answers

William TranWilliam Tran
You need to pass in a parameter x
and make sure Test has a space in it like 'Test ' + n

Try this:

thx.
 
public class StringArrayTest {
    public static String[]generateStringArray(Integer x){
        List<String> Test = new List<String>(); 
            for(Integer n=0; n<x; n++){
            Test.add('Test ' + n);}  
            }
        return (Test);
    }
}
This was selected as the best answer
Chris MarzellaChris Marzella
I am struggling to understand why you would need to pass "x". Also x never gets set to a value. How does this work?
William TranWilliam Tran
That's what the requirements are:

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.

this is method used by another class

for example

I can code StringArrayTest.generateStringArray(10) and this will return a list of string 
Test 0,
Test 1,
...
Test 6
Test 7

etc.

This is how you can code utility methods/helper methods.

Thx
William TranWilliam Tran
Type this in developer console and see the results.

System.debug(StringArrayTest.generateStringArray(10));

Thx
Willie Ha 3Willie Ha 3
System.debug(StringArrayTest.generateStringArray(10));