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
Squire Q KershnerSquire Q Kershner 

Not sure why my class won't pass evaluation

Good afternoon,
I can't seem to figure out why my class for the "Getting Started with Apex: Unit 1" won't pass evaluation.  The class appears to work as expected when I run via the Execute Anonymous window, but the checker fails.

Here is my class:

public class StringArrayTest{
    public static void generateStringArray(integer q){
        list<string> test = new list<string>();
            for(integer i=0; i < q;i++){
            test.add('Test '+i);
            system.debug(test[i]);
        }
    }
}

When I run StringArrayTest.generateStringArray(9); in the EAW, I get the return I expected.
Thoughts?
Best Answer chosen by Squire Q Kershner
Ajay RanawatAjay Ranawat

Looking at your code I found two changes that need to be done:

1) Insted of Void you will have to use List<String> return type, because Void method don't return values.

2) system.debug(test[i]); won't work in your case, as return should be List/Array of String.

Try this :

public class StringArrayTest{
    public static List<string> generateStringArray(integer q){
        list<string> test = new list<string>();
            for(integer i=0; i < q;i++){
                test.add('Test '+i);
            }
            return(test);
    }
}

Mark as best answer if it works for you.

All Answers

Peter_sfdcPeter_sfdc
The assessment challenge states the following: 
 
"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."
 
You've correctly constructed the strings for each iterations, but your method is declared as void, meaning nothing will be returned. You will need to refactor your method to meet the requirements above, and then it will pass the assessment. Good luck! 
Ajay RanawatAjay Ranawat

Looking at your code I found two changes that need to be done:

1) Insted of Void you will have to use List<String> return type, because Void method don't return values.

2) system.debug(test[i]); won't work in your case, as return should be List/Array of String.

Try this :

public class StringArrayTest{
    public static List<string> generateStringArray(integer q){
        list<string> test = new list<string>();
            for(integer i=0; i < q;i++){
                test.add('Test '+i);
            }
            return(test);
    }
}

Mark as best answer if it works for you.

This was selected as the best answer
Squire Q KershnerSquire Q Kershner
So that works, but I'm not understanding why.  What is the difference (other than the obvious) between my statement and the one posted?

Mine:   public static void generateStringArray(integer q){
Yours:  public static List<string> generateStringArray(integer q){

So I understand that a VOID method can't return anything, so that made sense.  But why does adding the datatype (List<string>) make it work?  I have a guess, but would like to hear it from the pros first.
Peter_sfdcPeter_sfdc
It is the difference between 
  • Output to log
  • Returning a value
In your solution you did output to log. That's fine. And that's what you were seeing in execute anonymous. But the way your wrote your method, you weren't passing any data back to the thing that called it. That's what return is for. 

The requirement was that the method needed to return a value, in other words, it needed to be able to pass data back to the code that called it. 

As such, the challenge/assessment was written in such a way as to expect the return statement. I expect the assessment writer was invoking the method, and then testing whether the correct data was returned. Something like this: 

List<String> stringsToTest = StringArrayTest.generateStringArray(10);
System.assert(10,stringsToTest.size());

With your original code, this test would either throw an exception (because you can't pass 'void' into List<String>) or it would always fail. 

Does that help? 

(By the way, did "return(test);" work? Because normally correct syntax would be "return test;.)