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
Ashok S 7Ashok S 7 

how can i solve the following requirment

Hai guys,
I have a requirment.
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.
As of my understanding i created apex class.

public class StringArrayTest  {   public static list<string> generateStringArray()     {         list<string> ss = new list<string>();         ss.add('Test 0');         ss.add('Test 1');         ss.add('Test 2');         ss.add('Test 3');         ss.add('Test 4');         ss.add('Test 5');         ss.add('Test 6');         ss.add('Test 7');         ss.add('Test 8');         system.debug('the list of strings are='+ss);         return ss;     }    }

but it is not staisfy the requirement .please help me any to slove the requirment
 
Best Answer chosen by Ashok S 7
William TranWilliam Tran

Ashok, you should read the exercise again so that you are confortable with the different aspect of the exercise.

The Apex class must be called 'StringArrayTest' and be in the public scope.
--> public class StringArrayTest{
The Apex class must have a public static method called 'generateStringArray'.
-->public static List<String> generateStringArray(Integer n) {

The 'generateStringArray' method must return an array (or list) of strings.
-->public static List<String> generateStringArray(Integer n) {

Each string must have a value in the format 'Test n' where n is the index of the current string in the array.
   s= 'Test ' + x;

The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.
  public static List<String> generateStringArray(Integer n) {


The whole solution:

public class StringArrayTest{

    public static List<String> generateStringArray(Integer n) {
        List<String> ListFields = new List<String>();
        String s;
        integer x;
        for (x=0;x<n;x++){
            s= 'Test ' + x;
            ListFields.add(s);}  
            return(ListFields);
    }
}

Also,

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks

All Answers

Suraj GharatSuraj Gharat
Hi Ashok,

Please try using loops (for, while). See below code.
 
public class StringArrayTest{
	public static list<string> generateStringArray(Integer count){
		List<String> ss=new List<String>();
		for(Integer i=0;i<count;i++)
			ss.add('Test '+i);
		return ss;
	}
}

 
William TranWilliam Tran

Ashok, you should read the exercise again so that you are confortable with the different aspect of the exercise.

The Apex class must be called 'StringArrayTest' and be in the public scope.
--> public class StringArrayTest{
The Apex class must have a public static method called 'generateStringArray'.
-->public static List<String> generateStringArray(Integer n) {

The 'generateStringArray' method must return an array (or list) of strings.
-->public static List<String> generateStringArray(Integer n) {

Each string must have a value in the format 'Test n' where n is the index of the current string in the array.
   s= 'Test ' + x;

The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.
  public static List<String> generateStringArray(Integer n) {


The whole solution:

public class StringArrayTest{

    public static List<String> generateStringArray(Integer n) {
        List<String> ListFields = new List<String>();
        String s;
        integer x;
        for (x=0;x<n;x++){
            s= 'Test ' + x;
            ListFields.add(s);}  
            return(ListFields);
    }
}

Also,

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
This was selected as the best answer
Ashok S 7Ashok S 7
Hai guys 
here another requirment 
The Apex class must be called 'AccountHandler' and be in the public scope.
The Apex class must have a public static method called 'insertNewAccount'.
The 'insertNewAccount' method must accept an incoming string as a parameter, name the account after the parameter, insert it into the system and then return the account record.
The 'insertNewAccount' method must also accept an empty string, catch the failed DML and return null.
i did not understand this requirment please explain me any one
William TranWilliam Tran
Ashok, please post a different question for each question, but here's the gist of it:


The Apex class must be called 'AccountHandler' and be in the public scope.
-->public class AccountHandler{

The Apex class must have a public static method called 'insertNewAccount'.
-->    public static account insertNewAccount(string s){

The 'insertNewAccount' method must accept an incoming string as a parameter,
 -->   public static account insertNewAccount(string s){


name the account after the parameter,

set the name of the account equal to the string s (try to do it). 

insert it into the system and then return the account record.

-->    insert acc;

The 'insertNewAccount' method must also accept an empty string, catch the failed DML and return null.

--> catch (DmlException e) {......... try to fill in the rest

Try putting it all together, if you still have issue, post a new question with what you got and either I or someone else will help.

Thx

By the way, be sure to give thumbs up to any answer that helped you.
Doing so will give encouragement for people for helping you out (it's like a thank you).
Ashok S 7Ashok S 7
thank u for helping