Reputation: 103
I have to write a method to switch the first and last letter of a string. For example the string "java" would become "aavj".
Here is what I have so far:
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 that code I get the following output "ajavaj" when I input "java", so how can I fix this? I need to switch the first two letters and still have the middle of the string. What should I do?
Upvotes: 4
Views: 3041
Reputation: 13912
Not much different than the other answers, but another way:
password = password.charAt(password.length() - 1)
+ password.substring(1, password.length() - 1)
+ password.charAt(0);
Upvotes: 1
Reputation: 969
You need to substring password on this line:
password = lastChar + password.substring(1, length-1) + firstChar;
Upvotes: 7
Reputation: 677
The way you were getting the first character was correct, but not the middle or last. Here's a solution showing all three cases:
String password = "123456789";
int length = password.length();
String firstChar = password.substring(0, 1);
String middle = password.substring(1, length - 1);
String lastChar = password.substring(length - 1, length);
password = lastChar + " " + middle + " " + firstChar;
System.out.println(password);
This will print:
9 2345678 1
Upvotes: 1
Reputation: 9648
By doing password = lastChar + password + firstChar;
you are concatenating the original password String with the two other Strings i.e. lastChar
& firstChar
. By doing this you will actually get a new String with lastChar
and firstChar
appended and not swapped.
Also, Strings are immutable and every time you are trying to manipulate it, you are ending up creating a new String. You should use char
array instead to avoid this problem.
Try this piece of code:
import javax.swing.JOptionPane;
public class printTest
{
public static void main(String[] args)
{
/* Capture Password */
String password = JOptionPane.showInputDialog("Please input your password");
char[] pass = password.toCharArray();
/* Swap Logic */
char temp = pass[0];
pass[0] = pass[pass.length - 1];
pass[pass.length - 1] = temp;
/* Show MessageDialog */
JOptionPane.showMessageDialog(null, new String(pass));
}
}
Upvotes: 3
Reputation: 23
I think this should do the trick:
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 password_changed = password.substring(1, password.length() - 1);
String firstChar = password.substring(0,1);
String lastChar = password.substring(length - 1);
password = lastChar + password_changed + firstChar;
JOptionPane.showMessageDialog(null, password);
}
}
You make an extra string (in this example password_changed) in wich you delete the first and last letter of the password variable. And you can use that new variable to change the password variable at the end.
Upvotes: 2
Reputation: 1294
char c[] = password.toCharArray();
char temp = c[0]
c[0] = c[password.length-1];
c[password.length-1] = temp;
There you go, swapping those two letters.
The c[0]
will be your first letter, you store it in a temp
variable, then modify the value of c[0]
( your first letter ) with the one in c[password.length-1]
( your last letter ) and then modify this value with the one store in the temp variable
Upvotes: 1