William Giron William Giron - 9 days ago 5
Java Question

How do I return 3 Strings in reverse order?

I'm using java programming, the user has already input 3 words as strings, word1, word2, and word 3. My task is to first capitalize all the words, example: run, roll, jump.....The words should become RUN, ROLL, JUMP. The problem is that I have to sort the words in reverse, example: JUMP, ROLL, RUN. I have to use an array, to sort them and then return the words how do I do this? this is what I have:

public static String reverseOrder(String word1, String word2, String word3) {
int a = word1.length();
int b = word2.length();
int c = word3.length();

String x;
String y;
String z;

x = word1.toUpperCase();
y = word2.toUpperCase();
z = word3.toUpperCase();

//this should be the output
String[] r = reverseOrder(word1,word2,word3);
System.out.println(Arrays.toString(r));
}
}

Answer
    import java.util.Arrays;
    class MyStringTest{
      public static String[] reverseOrder(String word1, String word2,    
                                    String word3) {
        // int a = word1.length();
        // int b = word2.length();
        // int c = word3.length();

        // String x;
        // String y;
        // String z;

        String x = word1.toUpperCase(); 
        String y = word2.toUpperCase();
        String z = word3.toUpperCase();


        String[] r = new String[]{x, y, z};
        // return the array
        return r;  // check the return type


      }
      public static void main(String [] args){
        String [] r = reverseOrder("run", "roll", "jump");
        System.out.println(Arrays.toString(r));


      }

    }
Comments