I need to write a method to switch the first and last letter of a string. For example, the string "java" will become "aavj".
Here is what I still have:
import javax.swing.JOptionPane;
public class printTest {
public static void main(String[] args) {
String password;
password = JOptionPane.showInputDialog("Please input your password");
int length = password.length();
String firstChar = password.substring(0, 1);
String lastChar = password.substring(length - 1);
password = lastChar + password + firstChar;
JOptionPane.showMessageDialog(null, password);
}
}
With this code, I get the following output of "ajavaj" when I enter "java", so how can I fix this? I need to switch the first two letters and still have the middle of the line. What should I do?
source
share