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
tanushree roy 6tanushree roy 6 

Create an Apex class that returns an array (or list) of strings. for Trailhead

tanushree...
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
 
Best Answer chosen by tanushree roy 6
Dilip_VDilip_V
Hi Tanu,

I tested the this code in my org and invoked it in using execute anonymous.
There is no problem at all.Its working as expected.
Make sure you saved the class before checking challenge or excuting using execute anonymous.
Plase execute StringArray class un=sing below line in execute anonymous and see the debug log.
Test.generateStringArray(5);​

Public Class  StringArrayTest
{
Public Static string[]  generateStringArray(integer index)
{

 List<String> moreColors = new List<String>();
  for(integer i=0;i<index;i++)
  { 
  moreColors.add('Test ' + i);
  }
 
    for(integer i=0;i<index;i++) 
 {
     
     System.debug(moreColors[i]);
 }
return morecolors;
}

}

Please let me know if it helps.
Thanks.
 

All Answers

Dilip_VDilip_V
Hi Tanu,

plase try below code
Public Class  StringArrayTest
{
Public Static string[]  generateStringArray(integer index)
{

 List<String> moreColors = new List<String>();
  for(integer i=0;i<index;i++)
  { 
  moreColors.add('Test ' + i);
  }
 
    for(integer i=0;i<index;i++) 
 {
     
     System.debug(moreColors[i]);
 }
return morecolors;
}

}
Thanks.

 
tanushree roy 6tanushree roy 6
Hi Dilip

Thanks for the rply
but still m getting error.
User-added image

what to do?
 
tanushree roy 6tanushree roy 6
When i check this code in anonymous window m getting this error. 
User-added image

What to do next. I couldnt find any mistake.. any one plz ...
Dilip_VDilip_V
Hi Tanu,

I tested the this code in my org and invoked it in using execute anonymous.
There is no problem at all.Its working as expected.
Make sure you saved the class before checking challenge or excuting using execute anonymous.
Plase execute StringArray class un=sing below line in execute anonymous and see the debug log.
Test.generateStringArray(5);​

Public Class  StringArrayTest
{
Public Static string[]  generateStringArray(integer index)
{

 List<String> moreColors = new List<String>();
  for(integer i=0;i<index;i++)
  { 
  moreColors.add('Test ' + i);
  }
 
    for(integer i=0;i<index;i++) 
 {
     
     System.debug(moreColors[i]);
 }
return morecolors;
}

}

Please let me know if it helps.
Thanks.
 
This was selected as the best answer
tanushree roy 6tanushree roy 6
Hi, Thermo Dynamics ,

Thanks for the rply..
 Its working fine. But i have doubt that why u r using index instead of integer?
Can u explain this to me...


Thanks and regards
Tanushree Roy