+ Start a Discussion
Ram Shiva KumarRam Shiva Kumar 

Trail Head Scenario

hi , From trail head scenario is like this,


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 my code is,


Public Class StringArrayTest
{


 public static List<String> generateStringArray(integer n){
        List<string> srtList = new List<string>();
        for(integer i=0;i<n;i++){
            string s = string.valueOf(i);
            srtList.add('\'Test '+i+'\'');
        }
        system.debug('hhh'+srtList);
        return srtList;

}

}

i have run the code every this coming exactly but when i check the  chalange in trail head iam getting the error ,error is asfollows,

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


Plesae any one tell me the suggestion.



Regards,
Siva 




 
Best Answer chosen by Ram Shiva Kumar
Dhanya NDhanya N
Hi Ram,

Please check this code:
public class StringArrayTest 
{
     //Public Method
    public static List<String> generateStringArray(Integer length) 
    {
        //Instantiate the list
        List<String> myArray = new List<String>();

        //Iterate throught the list
        for(Integer i=0;i<length;i++) 
        {
            //Populate the array
            myArray.add('Test ' + i);

            // Write value to the debug log
            System.debug(myArray[i]);

        } //end loop

        return myArray;

    }//end method   
}

Thanks,
Dhanya

All Answers

Dhanya NDhanya N
Hi Ram,

Please check this code:
public class StringArrayTest 
{
     //Public Method
    public static List<String> generateStringArray(Integer length) 
    {
        //Instantiate the list
        List<String> myArray = new List<String>();

        //Iterate throught the list
        for(Integer i=0;i<length;i++) 
        {
            //Populate the array
            myArray.add('Test ' + i);

            // Write value to the debug log
            System.debug(myArray[i]);

        } //end loop

        return myArray;

    }//end method   
}

Thanks,
Dhanya
This was selected as the best answer
Tavva Sai KrishnaTavva Sai Krishna
Hi ram,

Challenge is need to get the list of string in the format of "Test n". where as you are returning the string in the format of " 'Test n' ".  so delete the extra codes you are adding before and after the test n.

let me know if you face any issues. Also mark this answer as best answer if this answers you .

Thanks and Regards,
Sai Krishna Tavva.
Ram Shiva KumarRam Shiva Kumar
Hi Dhanya ,It worked for me .Thanks alot.


And Thanks to sai Krishna ,For the idea which is exactly true.