William Giron William Giron - 10 months ago 60
Java Question

Return the last letter of 3 strings capitalized

Hello I'm trying to run this program but I keep getting errors, I am using java. I am suppose to return the last letter of my already imputed string from a user and capitalize it. For example If my 3 strings are "run", "sofa" , and "computer" my output should be "NAR". Thank you

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 Source

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();