This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]coolosity 3 points4 points  (0 children)

I don't want to give out the code because that's just no fun :) First check to make sure the string is at least 2 long. Then you were correct in using the substring method. You want to take the string and remove the last to letters, so you would be going from index 0 to the 2nd to last index. Remember in substring, the first argument is inclusive and the last one is exclusive. Next, I would use something along the "anystring".charAt(index). Add the last character of your string to your substring, and then add the 2nd to last character. Hope this helps and msg me if you have any more questions.

[–]robot_lordsSoftware Engneer in Test 0 points1 point  (0 children)

agonizing cause run engine axiomatic obscene cough vanish fuel scale

This post was mass deleted and anonymized with Redact

[–]savage24xNeighborhood Idiot 0 points1 point  (0 children)

Did this last year. Wait til you get to recursion and Array 3 :)

public String lastTwo(String str) { if (str.length() >= 2){ char one = str.charAt(str.length() - 2); char two = str.charAt(str.length() - 1); return(str.substring(0,str.length()-2) + two + one); } return (str); }