+ Start a Discussion
dlCamelotdlCamelot 

Converting Integer to String

Hi All,

I'm trying to get an array of strings returned based on Integer n, but seem to keep getting a value instead of a string.  What do I need to adjust?  Thanks!

public class StringArrayTest {
    public void generateStringArray(){
        List<String>stringArray = new List<String>{};
        Integer n = 4;
        for(Integer i=n;i<stringArray.size();i++){
            String myInt = n.format();
            stringArray.add('Test' + myInt);
        }
         return stringArray;  
    }
}
RamuRamu (Salesforce Developers) 
Hi Desha, can you please provide a snapshot of the output you are expecting?
BWSBWS
The first thing I would do is check to see if the setup of your method "generateStringArray" matches what the Challenge specified.

Specifically ask yourself: 
  1. Is my method Public?
  2. Is my method Static?
  3. Does my method indicate that it returns a List of Strings?
  4. Does my method accept an input parameter that is an integer?
  5. Am I using the input parameter to determine how many strings I should add to the list returned by the method?
  6. While building the stings for the list am I formating them exactly as 'Test ' + n  
  7. If I tested my method and passed in a value of 3 for the input parameter does the list returned look like this? Test 0, Test 1, Test 2  
Ajay RanawatAjay Ranawat
Try this :

public class StringArrayTest{
    public static List<string> generateStringArray(integer q){
        list<string> test = new list<string>();
            for(integer i=0; i < q;i++){
                test.add('Test '+i);
            }
            return(test);
    }
}

Mark as best answer if it works for you.
bouscalbouscal
Deesha, Ajay's response will meet the requirements for the Trailhead unit.  You may want to look at the questions posed by BWS, specifically 'Does my method indicate that it returns a list of strings' which yours does not.
Please mark which ever of the above posts you feel most accurately leads you to the solution.

public class StringArrayTest {
    public void generateStringArray(){ // void methods return void
        List<String>stringArray = new List<String>{};
        Integer n = 4;
        for(Integer i=n;i<stringArray.size();i++){
            String myInt = n.format();
            stringArray.add('Test' + myInt);
        }
         return stringArray;  // now your asking your void method to return something
    }
}