简体   繁体   中英

My JOptionPane won't display my password

I want to write code for a project that asks a user for a username, address, and password then encrypts the password. 90% of the standalone class is just encryption, so feel free to breeze through it.

Here is the code for the standalone class:

import javax.swing.JOptionPane; //Import JOptionPane

public class UserInfo
{
  private String firsthalf, secondhalf, firsttwo, lasttwo, firstfourth, secondfourth, thirdfourth, finalfourth; //Declaring private variables
  private String username, useraddress, userpassword;
  private String encryptedpassword;
  public String encrypt, userinfo;

  public UserInfo()
  {
    String encryptedpassword = "";
    String username = "";
    String useraddress = "";
  }

  public void setUserPassword(String userpassword) //Get and Set methods
  {
    encryptedpassword = userpassword;
  }

  public String getUserPassword()
  {
    return encryptedpassword;
  }

  public void setUserName(String username)
  {
    username = username;
  }

  public String getUserName()
  {
    return username;
  }

  public void setUserAddress(String useraddress)
  {
    useraddress = useraddress;
  }

  public String getUserAddress()
  {
    return useraddress;
  }

  public String encrypt (String encryptedpassword) //Encrypt method
  {
    String removeWhitespaceAndConvertToUpper = "";
    String substitute = "";
    String swapHalfsForEncrypt = "";
    String swapFirst2WithLast2 = "";
    String swapMiddleChars = "";
    String toString = "";

    removeWhitespaceAndConvertToUpper (removeWhitespaceAndConvertToUpper);
    substitute (substitute);
    swapHalfsForEncrypt (swapHalfsForEncrypt);
    swapFirst2WithLast2 (swapFirst2WithLast2);
    swapMiddleChars (swapMiddleChars);
    toString (toString);

    return encryptedpassword;
  }
    public String removeWhitespaceAndConvertToUpper(String encryptedpassword)
    {
      encryptedpassword = encryptedpassword.trim(); //From here down is just encryption proccesses
      encryptedpassword = encryptedpassword.toUpperCase(); 

      return encryptedpassword;
    }

    public String substitute (String encryptedpassword)
    {

     encryptedpassword.replaceAll ("A" , "@"); characeters with required characters
     encryptedpassword.replaceAll ("E" , "=");
     encryptedpassword.replaceAll ("I" , "!");
     encryptedpassword.replaceAll ("J" , "?");
     encryptedpassword.replaceAll ("O" , "*");
     encryptedpassword.replaceAll ("P" , "#");
     encryptedpassword.replaceAll ("R" , "&");
     encryptedpassword.replaceAll ("S" , "$");
     encryptedpassword.replaceAll ("T" , "+");
     encryptedpassword.replaceAll ("V" , "^");
     encryptedpassword.replaceAll ("X" , "%");
     encryptedpassword.replaceAll (" ", "_");

     return encryptedpassword;
   }

   public String swapHalfsForEncrypt (String encryptedpassword) //Swapping halfs for encryption
   {

     String firsthalf = encryptedpassword.substring(0, encryptedpassword.length()/2); //2 substrings
     String secondhalf = encryptedpassword.substring(encryptedpassword.length()/2, encryptedpassword.length());

     encryptedpassword = (secondhalf + firsthalf); //Reversed substrings
     return encryptedpassword;
   }

   public String swapFirst2WithLast2 (String encryptedpassword) //Replaces last 2 digits with first 2 and vise versa
   {
     lasttwo = encryptedpassword.substring(encryptedpassword.length()-1, encryptedpassword.length()); //Last 2 variables
     firsttwo = encryptedpassword.substring (0, 1); //First 2 

     encryptedpassword = lasttwo + encryptedpassword.substring(2, encryptedpassword.length() - 2) + firsttwo;
     return encryptedpassword;
   }


   public String swapMiddleChars (String encryptedpassword)
   {
     int pwhalflength = encryptedpassword.length()/2;
     int lengthminus2 = pwhalflength - 2;
     int lengthplus2 = pwhalflength/2 + 2;
     int halfpassword = pwhalflength;
     int pwlength = encryptedpassword.length();

     firstfourth = encryptedpassword.substring (0, lengthminus2); //4 substrings for 4 parts of the password
     secondfourth = encryptedpassword.substring (lengthminus2, lengthplus2);
     thirdfourth = encryptedpassword.substring (halfpassword, lengthplus2);
     finalfourth = encryptedpassword.substring(lengthplus2, encryptedpassword.length());

     encryptedpassword = firstfourth + thirdfourth + secondfourth + finalfourth; //rearranging password

     return encryptedpassword;
    }

   public String toString (String encryptedpassword)
   {

     username += "Username: " + username + "\n";
     useraddress += "Address: " + useraddress + "\n";
     userpassword += "Password: " + encryptedpassword + "\n";

     return userpassword;
   }
}

And the code for my driver class:

import javax.swing.JOptionPane; //Importing JOptionPane

public class UserInfoDriver
{
  String encryptedpassword;
    public static void main (String[] args)
    {
      int again; //Variable for user controlled exit
      do
      {
      String userpassword = "";
      String encryptedpassword = "";
      String userinfo = "";
      String username, useraddress, password; //Declaring variables
      String firsthalf = "";
      String secondhalf = "";
      String firsttwo = "";
      String lasttwo = "";
      String firstfourth = "";
      String secondfourth = "";
      String thirdfourth = "";
      String finalfourth = "";
      int pwhalflength = userpassword.length()/2;
      int lengthminus2 = pwhalflength - 2;
      int lengthplus2 = pwhalflength/2 + 2;
      int halfpassword = pwhalflength;
      int pwlength = userpassword.length();

      username = JOptionPane.showInputDialog ("Enter your username: ");
      useraddress = JOptionPane.showInputDialog ("Enter your address: ");
      userpassword = JOptionPane.showInputDialog ("Enter your password: ");

      UserInfo encrypt = new UserInfo();
      UserInfo name = new UserInfo();
      UserInfo address = new UserInfo();

      encrypt.setUserPassword(userpassword); //Setting name, address, password
      name.setUserName(username);
      address.setUserAddress(useraddress);

      JOptionPane.showMessageDialog (null, "Your username is: " + username);
      JOptionPane.showMessageDialog (null, "Your address is: " + useraddress);
      JOptionPane.showMessageDialog (null, "Your password is: " + encryptedpassword);

      again = JOptionPane.showConfirmDialog (null, "Register again?");
    }
      while (again == JOptionPane.YES_OPTION); //User Controlled exit
  }
}

The code compiles and runs fine, but when I get to the JOptionPane where the password is supposed to show, nothing shows up. Everything else displays just fine, it's just the password. I was wondering what the problem was, and how to fix it. I know this isn't the best post you've ever seen because the majority is code, but any help at all would be appreciated.

This line

JOptionPane.showMessageDialog (null, "Your password is: " + encryptedpassword);

references encryptedpassword which you initialize to an empty string, but it is never changed again in your code. So naturally it will show nothing. Looking at your UserInfo class, it looks like you have an actual encrypt method that returns the encrypted password. You'll want to use this return value instead.

Also, on an unrelated note, I noticed you are creating a new UserInfo reference for each piece of info. I doubt this is going to be what you want as each piece of information you are setting via those references will all be contained in separate objects. The point of having a UserInfo class should be to allow you to have one reference to a single object with members that represent all the user's information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM