Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
Ashok 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.

Best Answer chosen by Ashok S 7
William 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;
return(ListFields);
}
}```

Also,

Thanks

Suraj 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++)
return ss;
}
}```

William 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;
return(ListFields);
}
}```

Also,

Thanks
This was selected as the best answer
Ashok 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 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 7
thank u for helping