+ Start a Discussion
SattibabuSattibabu 

Trailhead challenge(Create an Apex class that returns an array (or list) of strings.) is throwing error

Create an Apex class that returns an array (or list) of strings: 
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.

MyApexClass to above Challenge:

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


It's compile and execute as per requirement in Developer console. But Traihead showing the below 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.

Anyhelp would be greatly appreciated. Thanks.
 
Best Answer chosen by Sattibabu
Carolina Ruiz MedinaCarolina Ruiz Medina
Hi Sattibabu,
Please find below a little fix for your issue.

The problem was the space between Test and the number you had : Test0, Test1.... and what was necessary was Test 0, Test 1, ... 
public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n)
    {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)
        {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}

Also I will say that if in the test we pass through a negative value it will create values in the array then maybe for real purposes you would like to validate that.

Hope it it helpful :)

Regards,
Carolina. 

All Answers

Carolina Ruiz MedinaCarolina Ruiz Medina
Hi Sattibabu,
Please find below a little fix for your issue.

The problem was the space between Test and the number you had : Test0, Test1.... and what was necessary was Test 0, Test 1, ... 
public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n)
    {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)
        {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}

Also I will say that if in the test we pass through a negative value it will create values in the array then maybe for real purposes you would like to validate that.

Hope it it helpful :)

Regards,
Carolina. 
This was selected as the best answer
SattibabuSattibabu
Hi Carolina -- It's resolved my issue.  Thanks you so much for your assistance. 

If possible could you please explain in detail what does you mean by the below statement. I am not getting you fully 
" if in the test we pass through a negative value it will create values in the array then maybe for real purposes you would like to validate that. "

Please do the needful and thanks again for your great help. 
 
Carolina Ruiz MedinaCarolina Ruiz Medina
Hi Sattibadu, 
Sorry my bad , I was thinking in "while" instead "for". It will be fine always as the starting point is 0 :) Sorry for the misunderstanding, my bad I was thinking in 2 variables and "while" ( that is what happen to me when I try to do several things at the same time :P .. mix mix  ) 

Well.. Happy that is working now :) 


 
SattibabuSattibabu
Carolina - Thanks again for your reply and explanation.  I really grateful of you. Sorry for making you mix of several things :) 
Carolina Ruiz MedinaCarolina Ruiz Medina
Please not to worry at all, It was my bad. :)

Keep in touch and if I can help further I'll be happy to assist! Have a nice day!! 
shivram survaseshivram survase
public class StringArrayTest {
    public static String[] generateStringArray(Integer n)

    {
        List<String> listString = new List<String>();
         for(Integer i=0;i<n;i++)
        {
           listString.add('Test '+i);
            System.debug(listString[i]);
        }
        return listString;

    }

}
shivram survaseshivram survase
Hi guys,
Even i had an Problem,but solved .

Ur help was essential.And implemented this code.
italiahallitaliahall
Thank you Carolina!  The unit was making me doubt myself, but it taught me an important lesson about paying attention to the fine details.
Ghanshyam Yadav01Ghanshyam Yadav01
Hi, can anyone tell me that what is difference between String[] and List<String>,


Regards,
Ghanshyam
monika sdfcmonika sdfc
Hi Ghanshyam ,

List<String> xx = new List<String>();
String[]  xx = new List<String>(); both are same.Here we are declaring the list string variable.
The repersentation is different.
Ghanshyam Yadav01Ghanshyam Yadav01
Thanks Monika
mecrin luvismecrin luvis
I am not able to run that programme in developer consloe
mecrin luvismecrin luvis
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.
Pankaj Verma 07Pankaj Verma 07
My answer using while

public with sharing class StringArrayTest{
    
    public StringArrayTest(){
    
    }
    public static List<string> generateStringArray(integer n){
     List<string> mystring= new list<string>();
     integer i=0;
         while(i<n){
       
      
        mystring.add('Test '+i);
     
         i++;
         }
         system.debug('THIS IS MY FIRST TRAILHEAD OF APEX - '+mystring);
         return mystring;
         
    }
}
Pankaj Verma 07Pankaj Verma 07
public with sharing class StringArrayTest{
    
    public StringArrayTest(){
    
    }
    public static List<string> generateStringArray(integer n){
     List<string> mystring= new list<string>();
     integer i=0;
         while(i<n){
      
        mystring.add('Test '+i);
       
         i++;
         }
         system.debug('THIS IS MY FIRST TRAILHEAD OF APEX - '+mystring);
         return mystring;
         
    }
}
Sahana RasiahSahana Rasiah
Hi, can anyone tell me where is my error ?

apexCode

When I execute this, I have this error message:
Line: 2, Column: 1
Static methods cannot be invoked through an object instance: generateStringArray(Integer)


Any help would be greatly appreciated. Thanks.
Uvais KomathUvais Komath
@sahana rasiah You just have to click check challenge for completing it. no need to invoke from execute window.
However if u wish to see for yourself you need to invoke the method using the class name not an object
Uvais KomathUvais Komath
StringArrayTest.generateStringArray(5);
is what`s required
Sahana RasiahSahana Rasiah
@Uvais Komath Yes thank you, I forget this detail (class name not an object) but when I click check challenge for completing I always have this 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.
Uvais KomathUvais Komath
@sahana rasiah Replace "Text" with "Test" as per challenge requirements.
Sahana RasiahSahana Rasiah
@Uvais Komath Ah yes, you are correct.Thanks!
Satyanarayana PusuluriSatyanarayana Pusuluri
Hi,

Below code working fine for this Challenge  

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

Thanks & Regards,
Satya P
Divya YadavDivya Yadav


Line: 1, Column: 14
Variable does not exist: myArray

I am this above error, Can anyone help me to solve this ???

Regards
Divya
Uvais KomathUvais Komath
@divya mind sharing your code?
Divya YadavDivya Yadav
Uvais plz chk the code :

public class StringArrayTest{
    public static List<String> generateStringArray(Integer n)
    {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<5;i++)
        {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}
Uvais KomathUvais Komath

I see nothing wrong with your code except the following mistake.

 

Instead of  

for(Integer i=0;i<5;i++)

Use

for(Integer i=0;i<n;i++)

Trailhead might be calling your function with a value passed greater than 5 and might not be seeing test output above 5.

please check that and letme know

piyush sonipiyush soni
public class StringArrayTest {
   
    public static List<String> generateStringArray(Integer n){
    
        List <String> myArray = new List<String>();
       
        for(Integer i =0; i < n ;i++)
        {
            myArray.add('Test' + i);
            System.debug(myArray[i]);
        }
        
        return myArray;
    }
            
    
}

##### that is my code ,that's code  showing an error at the time of check challange ...please check this code and help me .
Carolina Ruiz MedinaCarolina Ruiz Medina
Hi Piyush,
It is failing because the challenge is checking for these strings: 'Text 0', 'Text 1' ... but you have 'Text0','Text1' ....
You need to add the space between the Text word and the number:
myArray.add('Test ' + i);

Hope it helps.

Carolina. 
piyush sonipiyush soni
Thank you so much Carolina!....now it's clear ...again, thanks alot..User-added image
Anne WiegersmaAnne Wiegersma
Thank you for your help Carolina. It gave me the last push I needed.
Carolina Ruiz MedinaCarolina Ruiz Medina
Thanks to you guys!! 
Happy that it was helpful!! :) 
Avinash YAvinash Y
Hi Carolina

Have a problem with my challenge.
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.

​My code goes like this:
public class StringArrayTest {

    public static list<string> generateStringArray(integer n){
    
        list<string> stringarray = new list<string>();
    

        for(Integer i=0;i<n;i++) {
            stringarray.add('Test'+i);
            System.debug(stringarray[i]);
}
            return stringarray;
}
}
Avinash YAvinash Y
Got it. Space after Test in stringarray.add('Test'+i);
Cool
vnookala23vnookala23
Can any help on this even I got the same error while doing the challenge 



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.


 
suji ysuji y
public class StringArrayTest {
    
    public StringArrayTest()
    {
        
    }
    public static String[] generateStringArray(integer n)
    {
         String[] lst = new String[n];
        if(n>0)
        {
      
        integer i=0;
        for(i=0;i<n; i++)
        {
        lst.add('Test '+i);
        System.debug(lst[i]);
        }
      
        }
          return lst;
       
    }
suji ysuji y
Please tell me why I am getting this 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.
Raul Cruz CarretoRaul Cruz Carreto
Esta es una posible solución

public class StringArrayTest {

    public static List<String> generateStringArray(Integer num){
        
        List<String> lista = new List<String>();
        
        for(Integer i=0; i<num; i++){
            lista.add('Test '+ i);
         }
        
        return (lista);
        
    }
    
}
Rajani KallaRajani Kalla
public class StringArrayTest {
    public static List<String> generateStringArray(Integer n){
        
        String sample='Test';
        list<String> temp=new List<String>();
            for(integer j=0;j<n;j++){
           temp.add(sample+j);
                System.debug(temp[j]);
            }
        return temp;
    }
}
vnookala23vnookala23
Rajani 

Try the below code

public class StringArrayTest {
    public static List<String> generateStringArray(Integer n)

    {
        List<String> listString = new List<String>();
         for(Integer i=0;i<n;i++)
        {
           listString.add('Test '+i);
            System.debug(listString[i]);
        }
        return listString;

    }

}
Krishna BARKrishna BAR
Please try this below code, it should work for you..

public class StringArrayTest
    {
        public static List<String> generateStringArray(Integer n)
        {
            List<String> myArray = new List<String>();
            for(Integer i=0;i<10;i++)
                    {
                       myArray.add('Test '+i);
                       System.debug(myArray[i]);
                }
            return myArray;
        }
    }
Krishna BARKrishna BAR
Please try this below code, it should work for you..

public class StringArrayTest
    {
        public static List<String> generateStringArray(Integer n)
        {
            List<String> myArray = new List<String>();
            for(Integer i=0;i<10;i++)
                    {
                       myArray.add('Test '+i);
                       System.debug(myArray[i]);
                }
            return myArray;
        }
    }
Ajitabh KUMARAjitabh KUMAR

Please try this:

this is coming as list of formatted string:
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;
    }
}

Balall YunusBalall Yunus
Perfect, just what I needed!
Suresh PalukuruSuresh Palukuru
Can some one confirm whetehr this challenge is working now.. As I'm facing below error even i have done correctly.
Error: Find the screen shot below;
User-added image
My Code
public class StringArrayTest {
     public static List<String> generateStringArray(Integer n) {
           List<String> liststring = new List<String>();
           for (Integer i = 0; i <= n; i++) {
                  liststring.add('Test ' + i);     
           }
          return liststring; 
      }
}
Geoffrey SchoeneckGeoffrey Schoeneck
Hi folks, I'm getting an error about "only top level methods can be declared static." Code is below and your help is appreciated.
public class StringArrayTest
	{ 
	public static list<string> generateStringArray(Integer n)
    	{ 
        List<String> myList = new List<String>(); 
        for(Integer i=0;i<n;i++)
        	{ 
            myList.add('Test '+i);
            system.debug(myList[i]);
        	} 
        return myList; 
    	} 
	}

 
Suresh PalukuruSuresh Palukuru
Hi Geoffery,

Change your peace of code as below
"for(Integer i = 0; i <= n; i++)" to "for (Integer i = 0; i <=n; i++)"
"public static list<string> generateStringArray(Integer n)" to "public static List<String> generateStringArray(Integer n)"

That should work.
Priya BiswalPriya Biswal
public class StringArrayTest {
    public static List<String> generateStringArray(Integer n)
    {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)
        {
            myArray.add('Test '+i);
            System.debug(myArray[i]);
        }
        return(myArray);
    }
}
kuldeep paliwalkuldeep paliwal
public class StringArrayTest {
    //in debug log its not run Only top-level class methods can be declared static
    //So on debug remove the static keyword then run it..
    public static String[]  generateStringArray(Integer n){
        List<String> myArray = new List<String>();
        for(Integer i=0; i<n; i++){
            myArray.add('Test ' +i);
            System.debug(myArray[i]);
        }
        return myArray;
    }

}
Marcello KeraMarcello Kera
Don't forget that APEX is case sensitive, so if you are declaring an object or a method return (like in this example List<String> is different from list<String>
cfernald7cfernald7
And FWIW, regarding the question from Ghanshyam on 6/10/15 about the difference between String[] and List<String>, I think it's primarily that declaring a List doesn't require also declaring an initial list size (i.e. number of elements) as does declaring an array. The list size wasn't given as a requirement and using that declarative gives more flexibility.
Rajeswari SankaranRajeswari Sankaran
can someone help me me why I am getting this 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.
Prerana ShikharePrerana Shikhare
Try this code  :

public class StringArrayTest {
    public static List <String>  generateStringArray(Integer n){
        
        List <String> testArray= new List<String>();
        
        
        for (Integer i=0;i<n;i++)
        {
           testArray.add('Test '+i);  //give space after Test
               
        }
     return(testArray)  ; 
    }
}
Prerana ShikharePrerana Shikhare
If above answer works for you please mark as Correct/Like.
 
dMaxSfDevdMaxSfDev
This worked for me and passed the challenge. Somehow, changing the increment in the for loop, to i=i+1 got it to pass. 

public class StringArrayTest {
       
    // Public static method
    public static List<String> generateStringArray(Integer n) {
    
    // Instantiate the String Array object
    List<String> testStringArray = new List<String>();
       
    // Iterate n times to create test string array
       for(Integer i=0; i<n; i=i+1) {
           testStringArray.add('Test '+ i);
       }
    
    return (testStringArray);   
    }
}
Ashok Kumar NayakAshok Kumar Nayak
public class StringArrayTest {
public static List<string> generateStringArray(Integer n){
List<string> stringArray=new List<string>();
for(Integer i=0;i<n;i++)
{
stringArray.add('Test '+i);
}
return(stringArray);
}
}

It works fine for me.
tanushree roy 6tanushree roy 6
I used this code but still m getting error...
where is my mistake can any one plz...


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


Thanks
Tanushree
dMaxSfDevdMaxSfDev
Hi Tanushree, can you please share the error that is displayed?

Just some things that I saw:
- The I in the integer for loop is not capitalized.
- The return array is not wrapped in parentheses, ().

Im not sure if the above things make a difference.
tanushree roy 6tanushree roy 6
I am getting this error only but in my org there is no error showing..

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.
dMaxSfDevdMaxSfDev
This is a solved discussion, so I'm not sure if this question goes here.

Try i=i+1 instead of i++ in the for loop.
Jan MikulasovicJan Mikulasovic
Hi

I have issue with this challenge. I have tried many of codes from this dicsussion as addition to mine developed code. Code is working. When I use system.debug to printout values I got correct results however I can not finish this challenge as trailhead generated empty error for me. Any idea? 
User-added image
dMaxSfDevdMaxSfDev
Hey Jan, is this still an issue?

I'd check the connection from the trailhead to your developer org. And maybe if your class is a public class? 
Jan MikulasovicJan Mikulasovic
I have tried it today without any changes and it was working....odd...maybe it was something with a connection to trailhead. Thank you for advice
raushan kumar 34raushan kumar 34
public class StringArrayTest {
    
    public static string[] generateStringArray(integer n){
        
        List<string> l1=new LIst<String>(n);
        
        for(integer j=0; j<n; j++){
            l1.add(j,'\'Test '+j+'\'');
        }
        return (l1);
    }
}


try this code
Pavas MathurPavas Mathur
public class StringArrayTest 
{
    public static List<String> generateStringArray(Integer n)
    {
        List<String> arr = new List<String>();
        for(Integer a=0;a<n;a++)
        {
            arr.add('Test '+a);
            System.debug(arr[a]);
        }
        return arr;
    }
}

Try this one.
Amit Kumar 447Amit Kumar 447
Hi All,

I have create class as 'StringArrayTest' and code is also working fine but i am getting strange error:
"Challenge Not yet complete... here's what's wrong: 
No Apex class named 'StringArrayTest' was found."

Can someone please suggest on this?

Thanks
Dipali JagtapDipali Jagtap
Thanks a lot...!!
raju peddaraju pedda
 public static list<string> generateStringArray(Integer n)

Can anyone tell me why we should use " list <string> "above 
Spence WilsonSpence Wilson
Please help.  I have some kind of access violation... 
Line: 3, Column: 28
Method is not visible: void StringArrayTest.generateStringArray(Integer)

I have a backround in Java.  This works in Netbeans with the correct syntax replacements.  So Im not sure what I have wrong here but I have some kind of syntax discrepency.   

MethodIsNotVisible

Thanks in advance.  

SFDC Developer console....
public class StringArrayTest
{
    public static String [] generateStringArray(integer size)
    {   
        String [] locStrArray = new String [size];
		
        //set values in array...
        for (integer i = 0; i < size; i++)
        {
            locStrArray[i] = 'Test ' + i;
        }    

		//display array...
        for (integer i = 0; i < size; i++)
        {
            System.debug(locStrArray[i]);       //when in doubt, system out...
        }   
        
        return locStrArray;
    }
}

Open Execute Anonymous Window...
integer size =10;
String [] strArray = new String [size];
strArray = StringArrayTest.generateStringArray(size);

        for (integer i = 0; i < size; i++)
        {
            strArray[i] = 'Test ' + i;
            system.debug('B ' + strArray[i]);      //when in doubt, system out...
        }

 
Suresh PalukuruSuresh Palukuru
This trails was very old.. moreover this error because of some technical issues.. whenever this occurred better contact to SFDC technical team instead replying here...
Spence WilsonSpence Wilson
Thanks I guess.  How do I contact them?  

For something thats such an extremely simple program to write I am somewhat disturbed that a bug so small remains unfixed. Perhaps this is tied to a larger rule..?

Is this even a valid challenge anymore?  I cant seem to find it.   
Dev Sandipam- (PSA)Dev Sandipam- (PSA)
public class StringArrayTest
{
    public static List<String> generateStringArray(Integer n)
    { 
       List<String> List1 = new List<String>();

        for(Integer i=0; i<=n; i++)

            List1.add('Test ' + i);

        return List1;

    }

}

This is my code.  throwing thhe same error again and again.  on anonymus de bug execution ,  by excluding class name and highlighting the code between, this is working. If it is really a bug, then how come many of the above comments sadi it's working?  Please help.  
Rajesh Sahoo 6Rajesh Sahoo 6
public class StringArrayTest {
    
    public static List<String> generateStringArray (Integer n){
        List<String> returnList = new LIST<String>();
        for(Integer i=0;i<=n;i++){
            returnList.add('Test ' + i);
            System.debug(returnList[i]);
        }
        return returnList;
    }
    
}


This is my Code. Executin witout error. But unable to pass trailhead challenge. Please help: 


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.
sure 36sure 36
Try this code it will work


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

 
sure 36sure 36
hi rajesh

try these if that dosent work

public class StringArrayTest { public static List<String> generateStringArray(Integer n) { System.debug(n); List<String> str = new List<String>(); for(Integer i=0;i<n;i++) { str.add('Test '+i); } System.debug(str); return str; } }
Suresh Surya 8Suresh Surya 8

Hi, On the same code, I get the following error
Line: 1, Column: 36
Unexpected token '('.

Code is:
public class StringArrayTest{
    public static List<String> generateStringArray(Integer n)
    {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)
        {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}
 

Executing as : StringArrayTest.generateStringArray(10)

Please help

Karina SantosKarina Santos
@Suresh,
Try executing like this: StringArrayTest.generateStringArray(10);
I was missing the ';' at the end too ;)
Gianluca MirabelliGianluca Mirabelli
Hi, 
I click on "check challenge", after some minutes I've this message:

"Looks like we're having issues, please try again. If this issue persists, please contact us using the submit feedback section on the sidebar."

Can someone help me to solve it?

Cheers
dMaxSfDevdMaxSfDev

Hi, This is a separate issue. Please create another thread with this question. Also, the initial question to this thread has been solved. 

Suggestion: Log out and log back into trailhead and check the challenge. 

Thank you! 

Lilith CostasLilith Costas
OK, I know some of you will laugh, but I'm going to add some comments about more basic issues I was having here that noobs may experience as well. And you wouldn't be on this exercise if you weren't a noob. First, don't create your code in the "Anonymous Execute Window", that doesn't count. Make sure you do File -> New -> Apex Class and write it in that window. And when you're done, make sure you do File -> Save, otherwise Trailhead won't realize you've done anything
saras chandrasaras chandra
Hi, On the same code, I get the following error
Line: 1, Column: 36
Unexpected token '('.

Code is:
public class StringArrayTest{
    public static List<String> generateStringArray(Integer n)
    {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)
        {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}
 
Executing as : StringArrayTest.generateStringArray(10)

Please help
Swetaleena Sahoo 9Swetaleena Sahoo 9
I am not that good at code.. but can we do below - 

myArray[i] = 'Test ' + i;
  
instead of  myArray.add('Test '+i);

It didnt work and gave out of bound exception. Just wanted to know if any code similar to this is there or it is fully wrong??
sorry for my ignorance :)
 
Darpan JadhavDarpan Jadhav
Try this..It worked for me:

public class StringArrayTest 
{
    public static List<String> generateStringArray(Integer n)
    {
        List<String> test=new List<String>();
        
        for (Integer i=0;i<n;i++) {
            test.add('Test ' + i);
        }
        
        System.debug(test);
        return test;
    }
}
Darpan JadhavDarpan Jadhav
Try this code...It worked for me:

public class StringArrayTest 
{
    public static List<String> generateStringArray(Integer n)
    {
        List<String> test=new List<String>();
        
        for (Integer i=0;i<n;i++) {
            test.add('Test ' + i);
        }
        
        System.debug(test);
        return test;
    }
}
Akash Dixit 8Akash Dixit 8
public class StringArrayTest
{
    public static List<String> generateStringArray(Integer n)
    {
        List<String> List1 = new List<String>();
        for(Integer i =0; i < n; ++i)
            List1.add('Test ' + i);
        return List1;
    }
}
Developer Console>Debug>Open Execute Anonymous Window 
And paste below code in it .
list<string> myArray = StringArrayTest.generateStringArray(10);
system.debug(myArray);
Please let us know if this will help you.
Thanks
Akash Dixit

 
Malhar_Ulhas_AgaleMalhar_Ulhas_Agale
public class StringArrayTest {
    public static List<string> generateStringArray(Integer it){
        list<string> strarry = new list<string>();
        // it is the integer n
        for(Integer i=0;i < it;i++){
            strarry.add('Test ' + i);            
        }
        system.debug(strarry);
        return(strarry);
    }

}
Hello to all,
I did it this way. It works too. Dont need to execute system.debug() statement all the time in the loop. Here it executes just one time only.
Prashant GantiPrashant Ganti
Hello Friends, 

I am getting same error as mentioned in many times above for the following code

Error: Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings

Code:
public class StringArrayTest {
    public static List<String> generateStringArray(Integer n){
        List<String> myarray=new List<String>();
        for(Integer i=0;i<=n;i++) {
            myarray.add('Test'+i);            
        }
        System.debug(myarray);
        return(myarray);
    }
}

Please do let me know for any mistake
Thank in advance
subhasis dharsubhasis dhar
Hi ,
User-added image
getting the same error :  Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.
Apex class
------------------
public class StringArrayTest  {
    
// Public static method
    public static list<string> generateStringArray(integer n) {
        List <string> output = new List<string>();
        for (integer i = 0; i <=n; i++) {
            output.add('Test'+ i);
                
        } 
       
       return output;
    }
  
}

Execute Anonymous window
----------------------------------------
list<string> myArray = StringArrayTest.generateStringArray(5);
system.debug(myArray);

However, in the APEX log , I can see status as "Success".  But as soon as I click on "check challenge for 500 points" green button on trailhead screen, new lAPEX log shows up with following error :
"EXCEPTION_THROWN [1]|System.AssertException: Assertion Failed: Expected: 11, Actual: 10" 

check screenshot attached.
Martin LöfflerMartin Löffler

Hello everybody,

Swetaleenas hint did it for me. 

First entered it with the add-method from the list-object, didn´t work, but with the Array-Style [] it worked... funny thing is, this is explained as synonymous...

Greetings Martin 

R CampbellR Campbell
To anyone who goes all the way down this list changes their code, and tries the code given that is represented as working and are still seeing the same error. I did the same, following several other forum links, all with the same errors or worse. 

It was only in later modules that my fix appeared. 
If all else fails, create a new developer org, the processes and flows created for contacts in previous trails and modules will cause this to fail. deactivating the Processes and Flows in the org I was using got me through those, but for this specific one, it took a whole new org,

This code worked perfectly in a new org and failed in the old one.
Hope this helped someone, Soon I will think that bulling through this helped me, but still a little sore from the battle. 

public class StringArrayTest {
    public static List<String> generateStringArray(Integer n)   {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)   {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}
shane williams 16shane williams 16
Create an Apex class with a method 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
The method must accept an incoming Integer as a parameter, which will be used to determine the number of returned strings
The method must return a string value in the format Test n where n is the index of the current string in the array
shane williams 16shane williams 16
i have tried using the below solution but i was getting an error  "static can only be used on methods of a top level type"
public class StringArrayTest 
{
    public static List<String> generateStringArray(Integer n)   {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)   {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}

Can you please let me know 
Anubhav Ghosh 17Anubhav Ghosh 17
public class StringArrayTest 
{  
    //The generateStringArray  function should return a list of Strings|| List<String> and accept an Integer
    public static List<String> generateStringArray(Integer n) //Remember: there is not int in Apex.
    {
        //Create the list that will be returned
        List<String> myList = new List<String>();
        
        //According to the challenge, list should have n items in the form of 'Test n1', 'Test n2' and so on.
        for(Integer i=0; i<n;i++)
        {
            myList.add('Test '+i);
            System.debug('Current String: '+myList.get(i));
        }
        return myList;  
    } 
}
I HAVE EXPLAINED THIS WITH COMMENTS WRT THE CHALLENGE