You need to sign in to do that
Don't have an account?
Rohit Tripathi
Create an Apex class that returns an array (or list) of strings.
Hello,
This is the trailhead questions which I am trying to solve :
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.
Here is my code :
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+'\'' );
}
System.debug(List1);
return List1;
}
}
I am getting following 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.
I tried it many times but I am not able to solve this problem. Please help.
This is the trailhead questions which I am trying to solve :
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.
Here is my code :
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+'\'' );
}
System.debug(List1);
return List1;
}
}
I am getting following 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.
I tried it many times but I am not able to solve this problem. Please help.
All Answers
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.
public static String[] generateStringArray(Integer length) {
String[] myArray = new List<String>(Integer length)
for(Integer i=0;i<length;i++) {
myArray.add('Test ' + i);
System.debug(myArray[i]);
}
return myArray;
}
}
copy the above code.It should work....
The code line in the above code should be:
myArray.add('Test' +' '+i);
It should work now.
Suggestions?
public class StringArrayTest {
public static String[] generateStringArray(integer len) {
String[] myArray = new List<String>();
for(Integer i=0;i< len;i++) {
myArray.add('Test ' +i);
System.debug(myArray[i]);
}
return myArray;
}
}
I was able to execute the coded successfully provided Rajkumar Rao, but i am not sure where to see the output of the code.
Line: 3, Column: 28
Only top-level class methods can be declared static
In anonymous window type the following to get the output,and then select debug only.
StringArrayTest.generateStringArray( any integer);
for example:
StringArrayTest.generateStringArray(9);
Error: Find the screen shot below;
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;
}
}
Just change the "for(Integer i = 0; i <= n; i++)" to "for (Integer i = 0; i <=n; i++)"
Can someone please let me know what is wrong. I am able to successfully execute the method in the Developer Console. I have an image below, but the challenge in Trailhead is still not accepting my method. I am using a List instead of an Array.
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.
Please try above code
Seeking for help.
public class StringArrayTest {
public static list<string> generateStringArray(integer n){
//n=5;
list<string> mylist=new list<string>();
for(integer i=0;i<n;i++){
string s1='test'+ i;
mylist.add(s1);
system.debug(mylist[i]);
}
return mylist;
}
}
Dont know what is wrong with above code.getting 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.
any help would be appreciated.
Thanks
The issue is with line string s1='test'+ i...
Because the result must be ('Test 0', 'Test 1', ...). You need to put a space in between :-)
string s1='Test'+ ' ' + i;
But I changed liststring.add('Test ' + i); ---> liststring.add('Test' + ' ' + i); and it all worked :-)
But I dont know why it didnt work as the code is fine with liststring.add('Test ' + i); (a space after Test). It might be with the Saleforce code-checker is coded like that
wrote the code , got this msg and i copied from above and still the same
Chalenge 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.....
Please help
Use: i=i+1 instead of i++
Because the result must be ('Test 0', 'Test 1', ...). You need to put a space in between :-)
string s1='Test'+ ' ' + i;
It workes for me.
please help,
i am getting following error in my org
Error: Compile Error: Variable does not exist: i at line 7 column 16
public class StringArrayTest
{
public static List<String> generateStringArray(Integer n)
{
list<String> a=new List<String>();
for(integer i=0; i<n; i++);
a.add('Test' + i);
return a;
}
}
public static List<String> generateStringArray(Integer n){
List<String> strings = new List<String>();
integer count = 0;
While(count < n){
strings.add('Test ' + count);
count ++;
}
System.debug(strings);
return strings;
}
}
public class StringArrayTest {
public static List<String> generateStringArray(Integer n){
List<String> strings = new List<String>();
integer count = 0;
While(count < n){
strings.add('Test '+count);
count ++;
}
System.debug(strings);
return strings;
}
}
StringArrayTest.generateStringArray(n);
Ex: StringArrayTest.generateStringArray(2);
public class StringArrayTest{
public static List<String> generateStringArray(Integer n){
List<String> returnList=new List<String>();
String value;
if (n<=0){
value='test'+n;
returnList.add(value);
}
else{
for (Integer i=0;i<n;i++){
value='test'+i;
returnList.add(value);
}
}
System.debug(returnList);
return returnList;
}
}
Challenge Not yet complete... here's what's wrong:
No Apex class named 'StringArrayTest' was found
Below is the code I wrote which is not very different from the code given by RajKumar Rao. Can anyone please point me out the defect in my code?
public class StringArrayTest
{
public static String[] generateStringArray(Integer n)
{
String[] my_list = new List<String>();
for (integer i=0; i<n; ++i)
{
my_list.add('Test'+''+i); //concatenate the iterated i
System.debug(my_list[i]);
}
return my_list;
}
}
public class StringArrayTest{
public static List<String> generateStringArray(Integer n)
{
List<String> liststring=new List<String>();
//n=10;
for (Integer i=0;i<=n;i++)
{
liststring.add('Test' + ' ' + i);
}
system.debug(liststring);
return liststring;
}
}
when i am running this through Developer Console
StringArrayTest.generateStringArray(5);
i am getting below output
DEBUG|(Test 0, Test 1, Test 2, Test 3, Test 4, Test 5)
But when i am checking chhalenge i am getting below error
Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.
public static list<string> generateStringArray(integer temp)
{
list<string> l=new list<string>();
for(integer i=0;i<temp;i++)
{
l.add('Test '+i);
}
return l;
}
}
Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.
The issue is Post-increment. It should be pre-increment.
Change this line,
To this
public static List<String> generateStringArray(integer num){
List<String> Returnlist = new List<String>();
For(integer i=0; i<num; i++){
Returnlist.add('Test '+i);
System.debug(Returnlist[i]);
}
Return Returnlist;
}
}
public static List<String> generateStringArray(integer num){
List<String> Returnlist = new List<String>();
For(integer i=0; i<num; i++){
Returnlist.add('Test '+i);
System.debug(Returnlist[i]);
}
Return Returnlist;
}
}
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 arrayLength) {
List<String> stringArray = new List<String>();
for(Integer i = 0;i < arrayLength;i++){
stringArray.add('Test ' + i);
System.debug(stringArray[i]);
}
return stringArray;
}
}
I have added debug statements too.
public class StringArrayTest {
// Public method
public static string[] generateStringArray (Integer num) {
List<string> callList = new List<string>();
for (integer i=0;i<num;i++){
callList.add('Test'+' '+ i);
system.debug(callList[i]);
}
return callList;
}
}
Once you add Apex class, you can try below anonymous APEX to execute it.
StringArrayTest.generateStringArray(4);
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;
}
}
its successful,but i found this as Chalenge 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....
public class StringArrayTest {
// Public method
public static String[] generateStringArray(Integer value) {
String[] Test1 = new List<String>();
for(Integer i=0;i<value;i++) {
// Add value to the array
Test1.add('Test '+i);
System.debug(Test1[i]);
}
return Test1;
}
}
Please save it and refresh the trail and then try to complete the Challenge.
<Challenge not yet complete in My Trailhead Playground 1
Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.>
public class StringArrayTest {
public void generateArray(Integer num1){
String[] numList1 = New List<String>();
numList1 = generateStringArray(num1);
}
public static String[] generateStringArray(Integer num2){
String[] numList = New List<String>(num2);
for(Integer i = 0; i<num2; i++){
numList[i] = 'List ' +i;
system.debug(numList.get(i));
}
system.debug(numList);
return numList;
}
}
for everyone who has = in statement for(Integer i=0;i<=length;i++) it doesnt work !
try without = and its done :)
like this: for(Integer i=0;i<length;i++) and dont forget has others things also right ...
public static List <String> generateStringArray(Integer n){
List<String> myList = new List<String>();
//loop
for(Integer i=0; i<n; i++) {
// Add the value into the list
myList.add('Test' + ' ' + i);
}
System.debug(myList);
return myList;
}
}
I have even created a new class and tried code from this page but I get the exact same error in the logs.
FATAL_ERROR System.AssertException: Assertion Failed: Expected: Test0, Actual: Test 0
The current code is from this page.
public class StringArrayTest
{
public static String[] generateStringArray(Integer n)
{
String[] List1 = new List<String>();
integer count = 0;
While(count < n)
{
List1.add('Test' + count);
count ++;
}
System.debug(List1);
return List1;
}
}
Exactly as my original code was but I didn't have a space between the two apostrophes in
myList.add('Test' + ' ' + i);
Took re-reading your code to see it hahaha, thanks!!! I am such a noob at this! ;)
thankyou
If anyone looking at the correct working code, here it is.
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);
}
}
Thanks!
public class StringArrayTest {
public static List<string> generateStringArray(integer noofreturns){
List<String> names =new List<String>();
for(integer i=0;i<noofreturns;i++){
names.add('Test'+' '+i);
}
system.debug(names);
return names;
}
}
Thanks!
Ex: StringArrayTest.generateStringArray(8);
public class StringArrayTest
{
public static list<string> generateStringArray(integer para)
{
integer counter;
list<string> list1 = new list<string>();
for(counter=0; counter<para; counter++)
{
list1.add('Test '+counter);
}
system.debug(list1);
return list1;
}
}
// anonymous window code
StringArrayTest.generateStringArray(4);
public static List<String> generateStringArray(Integer n)
return StringArray;
Thanks in Advance
public static List<String> generateStringArray(Integer num){
List<String> myList = new List<String> ();
for(Integer i=0;i<num;++i)
myList.add('Test'+' ' +i);
return myList;
}
}
This "public static List<String> generateStringArray(Integer n)" is the definition of your method which returns the myList array as per the interger "num" which is a no elements in the list and it will look like the following string values ( 'Test 0', 'Test 1','Test 2', 'Test 3' ) depending on tthe number of elemnts you want to be generated in the list using the value 'num'
public static List<String> generateStringArray(Integer n) {
// List variable creation
List<String> myList = new List<String>();
// Adding element to the list based on integer n value
for (Integer i=0; i<n; i++){
myList.add('Test'+' '+i);
System.debug(myList[i]);
}
return myList;
For me above coding worked. Try it for whomsoever it not working.
public static String[] generateStringArray(Integer n)
{
String[] s = new String[]{};
for(Integer i=0; i<n; i++)
{
s.add('Test'+' '+i);
}
return s;
}
}
// This Code Working for me, I tried by myself for that trail
for (Integer i = 0; i < n; i++) {...}
1)
- Calling the helping method from calling method
- Returning and Copying the list from helping method into new list in calling method
// Main Class
public class StringArrayTest {
// Calling Method
public static void printListItem(Integer n) {
List<String> strListCopy = generateStringArray(n);
for (Integer i=0;i<n;i++) {
System.debug(strListCopy.get(i));
}
}
// Helping Method
public static List<String> generateStringArray (Integer n) {
List<String> strArrayList = new List<String>();
for (Integer i=0;i<n;i++) {
strArrayList.add('Test ' + i);
}
return strArrayList;
}
}
2)
What the above code that you wrote is not returning elements from list, use System.debug(List_item) inside loop inspite of out of the loop as it is not returning the list with it's name
// Main Class
public class StringArrayTest {
// Calling Method as well the Main Method
public static List<String> generateStringArray (Integer n) {
List<String> strArrayList = new List<String>();
for (Integer i=0;i<n;i++) {
strArrayList.add('Test ' + i);
System.debug(strListCopy.get(i));
}
return strArrayList;
}
}
public static List<String> generateStringArray(Integer num){
List<String> numString = new List<String>();
for(Integer i=0; i < num; i++){
numString.add('Test '+i);
}
System.debug(numString);
return numString;
}
}
=================================
StringArrayTest em = new StringArrayTest();
em.generateStringArray(5);
Use this below code you will get 100% result.
public class StringArrayTest {
public Static List<String> generateStringArray(Integer n){
List<String> lists = new List<String>();
for(Integer i=0;i<n;i++){
String x = 'Test'+' '+i;
lists.add(x);
System.debug(lists[i]);
}
return lists;
}
}