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 

I need help with java to apex

I need help with converting this java code:

public class Example{
  public static void reverseWordInMyString(String str){
   String[] words = str.split(" ");
   String reversedString = "";
   for (int i = 0; i < words.length; i++){
       String word = words[i];
       String reverseWord = "";
       for (int j = word.length()-1; j >= 0; j--){
  
         reverseWord = reverseWord + word.charAt(j);
      }
     reversedString = reversedString + reverseWord + " ";
   }
   System.out.println(str);
   System.out.println(reversedString);
  }
}

 

so far this is what i got :

public class Example {
    public static void reverseWordInMyString(String str){
        String[] words = str.split(' ');
        String reversedString = '';
        Integer i;
        Integer j;
        for(i=0; i<words.length(reversedString); i++){
            String reverseWord="";
            for(j = words.length(reverseWord)-1; j>=0; j--){
                reverseWord=reverseWord + word.charAt(j);
            }
            reversedString = reversedString + reverseWord + "";
        }
        System.debug(str);
        System.debug(reversedString);
    }
}

Best Answer chosen by Hannah Campbell
Steven NsubugaSteven Nsubuga
public class Example {
    public static void reverseWordInMyString(String str){
        String[] words = str.split(' ');
        String reversedString = '';
        Integer i;
        Integer j;
        for(i=0; i<words.size(); i++){
            String reverseWord='';
            String word = words[i];
            for(j = word.length()-1; j>=0; j--){
                reverseWord=reverseWord + word.substring(j, j+1);
            }
            reversedString = reversedString + reverseWord + '';
        }
        System.debug(str);
        System.debug(reversedString);
    }
}

 

All Answers

Steven NsubugaSteven Nsubuga
public class Example {
    public static void reverseWordInMyString(String str){
        String[] words = str.split(' ');
        String reversedString = '';
        Integer i;
        Integer j;
        for(i=0; i<words.size(); i++){
            String reverseWord='';
            String word = words[i];
            for(j = word.length()-1; j>=0; j--){
                reverseWord=reverseWord + word.substring(j, j+1);
            }
            reversedString = reversedString + reverseWord + '';
        }
        System.debug(str);
        System.debug(reversedString);
    }
}

 
This was selected as the best answer
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
<pre>
public class Example
{
    public static void reverseWordInMyString( String str )
    {
        List<String> words = str.split( '\\s' );
        List<String> sdrow = new List<String>();
        for ( String word : words ) sdrow.add( word.reverse() );
        String reversedString = String.join( sdrow, ' ' );
        System.debug( str );
        System.debug( reversedString );
    }
}
</pre>