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
Hannah CampbellHannah Campbell 

Need help with using the reverse method

Im having an error with my code. Not really familiar with reverse method. Need help with checking this and creating a simple test class.

this is the code that I got so far:

public class Example {
    public static void reverseWordInMyString(String str){
        List<String> str = new List<String>{''};
            List<String> reversedString = new List<String>();
        if(!str.isEmpty()){
            for (String s : str){
                reversedString.add(s.reverse());
            }
        }
        System.debug(str);
        System.debug(reversedString);        
    }    
}

Thanks in advance

Best Answer chosen by Hannah Campbell
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Hannah,  Are you sure Amit's solution works?  It will reverse the entire string, not the individual words in the string.  What are you trying to accomplish?  If you want to reverse the entire string, the much simple way to do so is this:
 
public class Example {
    public static void reverseWordInMyString(String str){
        String reversedString = str.reverse();
        System.debug(str);
        System.debug(reversedString);        
    }    
}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


Sample Test Class for you
@istest
public class ExampleTest
{
    static testmethod void validateselectWarehouse()
	{
		Example.reverseWordInMyString('Test Record');
    }
}


Let us know if this will help you
 
Amit Chaudhary 8Amit Chaudhary 8
Update your code like below to fix your code
public class Example {
    public static void reverseWordInMyString(String strName){
        List<String> str = new List<String>{strName};
            List<String> reversedString = new List<String>();
        if(!str.isEmpty()){
            for (String s : str){
                reversedString.add(s.reverse());
            }
        }
        System.debug(str);
        System.debug(reversedString);        
    }    
}

Let us know if this will help you
 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Hannah, the error is that the method takes a String parameter called 'str', and then the code declares a List<String> called 'str'.  You can't reuse a variable name like that.  I think the code below does what you're looking for (both class and test class).
 
public class Example
{
    public static String reverseWordInMyString( String str )
    {
        List<String> words = str.split( '\\s' );    //  splits on whitespace
        List<String> sdrow = new List<String>();
        for ( String word : words ) sdrow.add( word.reverse() );
        return String.join( sdrow, ' ' );
    }
}

@isTest
public class ExampleTest
{
    @isTest
    public static void testReverseWordInMyString()
    {
        String input = 'This is a test string.';
        String expectedOutput = 'shiT si a tset .gnirts';
        Test.startTest();
        String actualOutput = Example.reverseWordInMyString( input );
        Test.stopTest();
        System.assertEquals
        (   expectedOutput
        ,   actualOutput
        ,   'reverseWordInMyString() did not produce the expected output'
        );
    }
}

 
Hannah CampbellHannah Campbell
Thank you! it worked. and thanks for the references as well :)
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Hannah,  Are you sure Amit's solution works?  It will reverse the entire string, not the individual words in the string.  What are you trying to accomplish?  If you want to reverse the entire string, the much simple way to do so is this:
 
public class Example {
    public static void reverseWordInMyString(String str){
        String reversedString = str.reverse();
        System.debug(str);
        System.debug(reversedString);        
    }    
}

 
This was selected as the best answer