You need to sign in to do that
Don't have an account?
Sattibabu
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.
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.
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, ...
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
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, ...
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.
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.
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 :)
Keep in touch and if I can help further I'll be happy to assist! Have a nice day!!
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;
}
}
Even i had an Problem,but solved .
Ur help was essential.And implemented this code.
Regards,
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.
Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.
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;
}
}
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.
However if u wish to see for yourself you need to invoke the method using the class name not an object
is what`s required
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.
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
Line: 1, Column: 14
Variable does not exist: myArray
I am this above error, Can anyone help me to solve this ???
Regards
Divya
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;
}
}
I see nothing wrong with your code except the following mistake.
Instead of
Use
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
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 .
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.
Happy that it was helpful!! :)
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;
}
}
Cool
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 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;
}
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 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);
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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.
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);
}
}
//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;
}
}
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 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) ;
}
}
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);
}
}
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.
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
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.
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.
Try i=i+1 instead of i++ in the for loop.
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?
I'd check the connection from the trailhead to your developer org. And maybe if your class is a public class?
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
{
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.
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
Can anyone tell me why we should use " list <string> "above
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.
Thanks in advance.
SFDC Developer console....
Open Execute Anonymous Window...
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.
{
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.
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.
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; } }
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; } }
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
Try executing like this: StringArrayTest.generateStringArray(10);
I was missing the ';' at the end too ;)
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
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!
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
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 :)
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;
}
}
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;
}
}
And paste below code in it . Please let us know if this will help you.
Thanks
Akash Dixit
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.
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
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.
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
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;
}
}
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
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
public class StringArrayTest
{
public static List<String> generateStringArray(Integer input)
{
List<String> OutputList = new List<String>();
for(Integer i=0;i<input;i++) {
OutputList.add('Test '+i);
System.debug(OutputList[i]);
}
return OutputList;
}
}
One more question for some code in this unit, for the following code:
// Create an email message object
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {address};
mail.setToAddresses(toAddresses);
mail.setSubject(subject);
mail.setPlainTextBody(body);
-The last 3 lines, i guess they are passing values right? but what is the created "mail", a messaging list or what? and those setxxx are functions from system? like "add" ?
Thank you
public class StringArrayTest {
public static List<string> generateStringArray(integer n){
List<string> mystring=new List<string>();
for(integer i=0;i<n;i++)
{
mystring.add('Test '+i);
}
system.debug('This is my first trailhead of apex' +mystring);
return mystring;
}
LEAVE A SPACE
LEAVE A SPACE
LEAVE A SPACE
LEAVE A SPACE
LEAVE A SPACE
LEAVE A SPACE
LEAVE A SPACE
LEAVE A SPACE
LEAVE A SPACE
LEAVE A SPACE
Thank you @Carolina... I thought I was going to lose it.
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;
}
}
please find the rror in my code i am getting an error
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 static List<String> generateStringArray(Integer nums){
List<String> newArray = new List<String>();
System.debug(nums+'nums');
for(Integer i=0 ; i<nums ;i++){
newArray.add('Test '+ String.valueOf(i));
System.debug(newArray[i]);
}
return newArray;
}
}
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;
}
}
when i was executing the below Query on developer console i was getting error as below could you please someone help me.
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;
}
Getting error as below
Line: 2, Column: 32
static can only be used on methods of a top level type
https://grazeapk.com/kame-paradise-apk/
Most electric scooters (https://best-electricskateboard.com/evercross-evo8e-electric-scooter/) are made at a specific height and wires; therefore a function for adjustable electric scooters to be made. But as said earlier, most electric scooter brands didn’t have this facility.
We break down all the vital aspects before buying an electric skateboard. Be it your very first purchase or your next. Discussed in detail what matters and to look according to your skill level and age. Because all ages have different measurements and metrics, keeping in mind safety as riding an electric skateboard isn’t considered a safe and recommended ride. But nothing is wrong with it if you are looking for an electric scooter for fun and part-time commuting.
https://best-electricskateboard.com/