Switching the first and last letter of a string in Java?

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?

+4
source share
6 answers

You need to adjust the password in this line:

password = lastChar + password.substring(1, length-1) + firstChar;
+7
source

password = lastChar + password + firstChar;, String , .. lastChar firstChar. lastChar firstChar, .

, , , , . char.

:

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));
     }
}
+3

, :

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);
     }
    }

( password_changed), . .

+2
char c[] = password.toCharArray();
char temp = c[0]
c[0] = c[password.length-1];
c[password.length-1] = temp;

, . c[0] , temp, c[0] ( ) c[password.length-1] ( ), temp

+1

, , , . , :

  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);

:

9 2345678 1
+1

, -:

password = password.charAt(password.length() - 1)
           + password.substring(1, password.length() - 1)
           + password.charAt(0);
+1

Source: https://habr.com/ru/post/1626682/


All Articles