I am having a problem getting my first test result to work (which you can see below). When the string is "hello" it returns "llo" instead of "l". What is wrong in my code that is causing this to happen? My code and test results are as follows:
My code:
public class MiddleTest
{
/**
Gets the middle character or character pair from this string
when possible.
@param str a string
@return the middle character (if the string length is odd) or
the middle two characters (if it is even), or the empty string if str is
empty.
*/
public static String getMiddle(String str)
{
if ((str.length() % 2) == 0)
{
//Even length
if (str.length() > 2)
{
return str.substring( str.length() / 2 - 1, str.length() / 2 +1);
}
}
//Odd length
return str.substring(str.length() / 2);
}
}
Test Results
Test Results
Parameters Actual Expected Outcome
hello llo l fail
help el el pass
l l l pass
pass