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
Mark NemithMark Nemith 

Apex Basics and Database Module along the Beginner Developer Trail: List Challenge

Hi All,

I am working with a fresh DE org with no customizations and I am continually failing the first unit of the Apex Basics and Database Module along the Beginner Developer Trail.

The Challenge is below:

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.

...and this is my failing solution:
public class StringArrayTest {
    
	public static void generateStringArray (Integer rs){
        list<string> stringarray = new list<string>();
        
        for(Integer n=0;n < rs;n++){
            stringarray.add('Test ' + n);
        }
        
        string a = stringarray[0];
        string b = stringarray[1];
        string c = stringarray[2];
        string d = stringarray[3];
        
        system.debug(a);
        system.debug(b);
        system.debug(c);
        system.debug(d);
    
        integer y = stringarray.size();
        system.debug(y);
        
}
}

...and here is the failure message:

"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."

...All my returning debug values seem to be correct. Please help, if possible.

Thanks!

Mark
 
Best Answer chosen by Mark Nemith
James LoghryJames Loghry
Mark,

Your method is not returning anything.  The return type is void, instead of a List<String> like the last bullet point is asking for.  Change the return type and add a return statement around line 9, and you should be good to go.

All Answers

James LoghryJames Loghry
Mark,

Your method is not returning anything.  The return type is void, instead of a List<String> like the last bullet point is asking for.  Change the return type and add a return statement around line 9, and you should be good to go.
This was selected as the best answer
Mark NemithMark Nemith
James, thanks for the response...that worked.

Generally speaking, when is the void return type used?

Thanks for the help.
James LoghryJames Loghry
There are several cases where you may use void return types.  Cases such as when you pass in an object "by reference" and modify it based on some logic.  Cases where you may want to invoke a web service, but dont care what it returns.  Cases where you pass in a variable to update a record, etc.  Cases where you have a member variable in your controller and you want to perform some action on it, but don't care about a return type, etc etc.

 
JeffreyStevensJeffreyStevens
Mark - if your question was answered - be sure to selct one of James's comments as the correct answer.