William Giron William Giron - 6 days ago 6
Java Question

Return the last letter of three strings capitalized

I am trying to return the last letter of three user input strings and capitalize it.

If, for example, my strings are "run", "sofa" , and "computer" my output should be "NAR".

public static String lastLetters(String word1, String word2, String word3){

// I tried to capitalize all the words first and input them into a string
String x = word1.toUpperCase();
String y = word2.toUpperCase();
String z = word3.toUpperCase();

word1 = x.substring(x.lastIndexOf(' ') + 1);
word2 = y.substring(y.lastIndexOf(' ') + 1);
word3 = z.substring(z.lastIndexOf(' ') + 1);
String lastLetters = (word1, word2, word3);
return lastLetters;
}
}
//The output should be
System.out.println("The last letters of the words forms the word: " + lastLetters(word1,word2,word3));

Answer

consider this answer

public static void main(String[] args) throws Exception {

    String word1 = "run";
    String word2 = "sofa";
    String word3 = "computer";

    System.out.println(lastLetters (word1, word2, word3));

}

private static String lastLetters(String word1, String word2,
        String word3) {

    return (word1.substring(word1.length() -1) + 
            word2.substring(word2.length() -1) + 
            word3.substring(word3.length() -1)).toUpperCase();
}
Comments