73.4k views
11 votes
Write a method that returns a String that is just the first and last character of the given string Your return value should be only two characters long. You can assume that the given string will not be empty.​

User Nightking
by
4.0k points

1 Answer

6 votes

Answer:

The method in Java is as follows:

public static String returnlen(String input){

String output = ""+input.charAt(0)+input.charAt(input.length()-1);

return output;

}

Step-by-step explanation:

This defines the method

public static String returnlen(String input){

This concatenates the first and the last character of the input string

String output = ""+input.charAt(0)+input.charAt(input.length()-1);

This returns the concatenated string

return output;

}

User Ozan Yurtseven
by
4.4k points