I've worked through it several times, but I can't figure out how CodingBat's solution code correctly returns a two character string.
This is their solution:
public String frontBack(String str) {
if (str.length() <= 1) return str;
String mid = str.substring(1, str.length()-1);
// last + mid + first
return str.charAt(str.length()-1) + mid + str.charAt(0);
}
But why does this work for two character strings? If str = "AB" shouldn't the code return "B" (first part of return) + "B" (in this case wouldn't mid="B") and "A"...so "BBA"? I think I'm missing some logic with how String mid is created with a 2 character string. Thanks!
I recommend installing Eclipse or Other IDE and run the example in Debug. It's one of the best ways to learn JAVA. From your example I can tell you the following: Mid will be null because is making a substring from position 1 to 1 => the length = 0
For str = "AB"
Debug capture:

If the substring was from 1 to 2 then it would had showed letter B:

So you will only see as result str.charAt(str.length()-1) + str.charAt(0) Which is B and A.
It is easier to work with substring method if you think that letters are between indexes like shown below for "AB"
A B
^ ^ ^
0 1 2
and substring(a,b) would return characters placed between index a and b.
So when we execute code like:
"AB".substring(0,2) we get full "AB","AB".substring(0,1) we get "A"What seems to be confusing you is part when we execute code like: "AB".substring(1,1) OR (0,0) OR (2,2).
When we use same value for start and end index, then result of substring will be empty string "" because there are no characters between those indexes.
When str = "AB" and we execute
String mid = str.substring(1, str.length()-1);
then it is same as executing substring(1, 2-1) => substring(1,1) which means mid will be holding empty string "".
Now when we call
return str.charAt(str.length()-1) + mid + str.charAt(0);
it will be evaluated as
return 'B' + "" + 'A';
which means we will end up with
return "BA";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With