+ Start a Discussion
Nandhuk 8Nandhuk 8 

Apex basics & Database trailhead challenge i am getting an error.

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;
}
}
 i am not getting output like test 0,test1, test 2.....
can you correct my programming whether it is correct or not
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Nandhuk,

Greetings to you!

Your code is correct. Please be careful with the space in 'Test '+i when you add to the List.
listString.add('Test '+i);

Use 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;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Arun M 81Arun M 81
You can see the output only on system.debug
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);
return listString ;
}
}

Try this one.
Nandhuk 8Nandhuk 8
Hii Arun,
Void doesn't return type of value.So, it is getting error message.
Thankyou  for the reply
 
Nandhuk 8Nandhuk 8
Hi khan Anas,
I tried your method but  i don't have any space between the test and +i .Still, i am getting an error.
what should  to write or i recall method in the open anonymous debug mode.
Thank you for the reply
Arun M 81Arun M 81
Hi Nandhuk ,
Check My Screenshot, You want output like that?
Nandhuk 8Nandhuk 8
i got the same output but still getting error like that 
Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.
My Trailhead Playground 1
Launch
Check challenge to earn 500 points
Khan AnasKhan Anas (Salesforce Developers) 
Nandhuk, you need to pass one space between 'Test '+i 
Refer to my code.
Nandhuk 8Nandhuk 8
Hii,
I got the output and solved it.
Thank you for the support.
 
Khan AnasKhan Anas (Salesforce Developers) 
I’m glad I was able to help!

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue. *(After you choose the best answer the question is marked as “resolved”)*

Happy Learning!

Regards,
Khan Anas