Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的作业窗格不会显示我的密码_Java_Joptionpane - Fatal编程技术网

Java 我的作业窗格不会显示我的密码

Java 我的作业窗格不会显示我的密码,java,joptionpane,Java,Joptionpane,我想为一个项目编写代码,该项目要求用户输入用户名、地址和密码,然后对密码进行加密。90%的独立类只是加密,所以请随意浏览 以下是独立类的代码: import javax.swing.JOptionPane; //Import JOptionPane public class UserInfo { private String firsthalf, secondhalf, firsttwo, lasttwo, firstfourth, secondfourth, thirdfourth, f

我想为一个项目编写代码,该项目要求用户输入用户名、地址和密码,然后对密码进行加密。90%的独立类只是加密,所以请随意浏览

以下是独立类的代码:

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;
   }
}
以及我的驱动程序类的代码:

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
  }
}
代码编译并运行良好,但是当我进入密码应该显示的JopOptions窗格时,什么也没有显示。其他显示都很好,只是密码。我想知道问题是什么,以及如何解决它。我知道这不是你见过的最好的帖子,因为大部分都是代码,但是如果有任何帮助,我将不胜感激

JOptionPane.showMessageDialog空,您的密码为:+encryptedpassword

引用您初始化为空字符串的encryptedpassword,但它在代码中不会再次更改。因此,它自然不会显示任何内容。查看UserInfo类,看起来您有一个实际的加密方法返回加密密码。您将希望改用此返回值


另外,在一个不相关的注释中,我注意到您正在为每一条信息创建一个新的UserInfo引用。我怀疑这会是您想要的,因为您通过这些引用设置的每一条信息都将包含在单独的对象中。拥有UserInfo类的目的应该是允许您对单个对象进行一次引用,该对象的成员代表所有用户的信息。

当我使用JOptionPane.showMessageDialog null尝试此操作时,您的密码是:+encrypt,在消息框中我得到的密码是:UserInfo@2bf6e977encrypt是UserInfo类型的变量。您需要从该对象获取密码。我建议在这里读一读:我不确定我是否理解。我试图阅读链接,但不知道如何在这个项目中使用它。