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
SARANG DESHPANDESARANG DESHPANDE 

Error in trailhead challenge

I was solving the challenge of trailhead of Apex development:

The question was:
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.

My code is:


public class StringArrayTest {

    static List<String> TestArray= new List<String>();
    public static String[] generateStringArray(Integer n){
        
        for(Integer i=1;i<=n;i++){   
            TestArray.add('Test'+i);
        }
        
        return TestArray;
    }
    
}

I am able to get desired outpuut but still it is returning an 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.

Not able to understand what it want exactly
Best Answer chosen by SARANG DESHPANDE
Amit Chaudhary 8Amit Chaudhary 8
Problem in your code is that you need to pass one space between 'Test '+i and you need to start loop from zero

Please try below code:-
public class StringArrayTest {
        public static String[] generateStringArray(Integer length) {
        String[] myArray = new List<String>();
        for(Integer i=0;i<length;i++) {
           myArray.add('Test ' + i);

            System.debug(myArray[i]);
        } 
        return myArray;
    }     
}
And go :
Your Name>Developer Console>Debug>Open Execute Anonymous Window 
Paste below code in it
list<string> myArray = StringArrayTest.generateStringArray(10);
system.debug('************'+myArray);
Please let us know if this will help you

Thanks
AMit Chaudhary


 

All Answers

SARANG DESHPANDESARANG DESHPANDE
my code for execution:


List<String> Returnedstring =StringArrayTest.generateStringArray(5);
System.debug('Returned Array is: ');
For(Integer i=0;i<5;i++){
    System.debug(Returnedstring[i]);
}
Vla ChaVla Cha
Hi,

If the requirement is to ahve the test stings in form ('Test 0', 'Test 1', ...) then you would need to add a blank space while you are building the test data:
 
for(Integer i=1;i<=n;i++){   
            TestArray.add('Test '+i);
        }

I hope this helps ;)

Cheers!
 
William TranWilliam Tran
Your code execution should be this:
 
List<String> Returnedstring =StringArrayTest.generateStringArray(5);
System.debug('Returned Array is: ' + Returnedstring );

and your full class should be this:
 
public class StringArrayTest {

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

Thx
 
Amit Chaudhary 8Amit Chaudhary 8
Problem in your code is that you need to pass one space between 'Test '+i and you need to start loop from zero

Please try below code:-
public class StringArrayTest {
        public static String[] generateStringArray(Integer length) {
        String[] myArray = new List<String>();
        for(Integer i=0;i<length;i++) {
           myArray.add('Test ' + i);

            System.debug(myArray[i]);
        } 
        return myArray;
    }     
}
And go :
Your Name>Developer Console>Debug>Open Execute Anonymous Window 
Paste below code in it
list<string> myArray = StringArrayTest.generateStringArray(10);
system.debug('************'+myArray);
Please let us know if this will help you

Thanks
AMit Chaudhary


 
This was selected as the best answer
SARANG DESHPANDESARANG DESHPANDE
Thanks all for the help....got 500+ points :)
Hariharan ramyaHariharan ramya
public class StringArrayTest {

    public static list<String> generateStringArray (Integer n){
     
        List<String> arr = new List<String>();
        
        for(Integer i=0; i<n; i++){
            
           arr.add('Test ' + i);     // 'Test ' + i   
           System.debug(arr[i]);
        }
        
    return arr;        
    }
    
}
peddapuram shiva kumarpeddapuram shiva kumar
first you need to write this code in class creation


public class StringArrayTest {
        public static String[] generateStringArray(Integer length) {
        String[] myArray = new List<String>();
        for(Integer i=0;i<length;i++) {
           myArray.add('Test ' + i);

            System.debug(myArray[i]);
        } 
        return myArray;
    }     
}



after that you need to open debug >> open execution anonymously and then paste the following code then the issue gets resolved

list<string> myArray = StringArrayTest.generateStringArray(10); system.debug('************'+myArray);