Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I compare two passwords (create password and confirm password) useing the .equals() method?
    primarykey
    data
    text
    <p>I'm building a simple create account user GUI that is integrated with my LogInScreen class. It creates a simple User, serializes it, and then lets me use that access in my log in program. The problem is, when I type in my passwords, they're never correct. I always get a problem where my program tells me that my passwords are not the same. I'm not sure how to fix this, and I would like to know how. I'll post my code below(the whole thing, along with my serializing class because the problem may be here).</p> <p>User Class:</p> <pre><code>package passwordProgram; import java.util.ArrayList; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.Serializable; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.UIManager; public class User implements Serializable, ActionListener { public static ArrayList&lt;String&gt; allUsernames = new ArrayList&lt;String&gt;(); String username; String password; public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } User user = new User(); user.mainGUI(); } JFrame frame; JPanel panel; JTextField createUsername; JPasswordField createPassword; JPasswordField confirmPassword; JButton createAccount; JLabel noValid; public void mainGUI() { noValid = new JLabel(); frame = new JFrame("Create a new account!"); panel = new JPanel(); panel.setBackground(Color.ORANGE); createPassword = new JPasswordField(10); confirmPassword = new JPasswordField(10); createUsername = new JTextField(10); JLabel userTxt = new JLabel("New Username: "); JLabel userPass = new JLabel("New Password: "); JLabel confirmPass = new JLabel("Confirm Password: "); createAccount = new JButton("Create your account!"); panel.setLayout(new GridBagLayout()); GridBagConstraints left = new GridBagConstraints(); left.anchor = GridBagConstraints.WEST; GridBagConstraints right = new GridBagConstraints(); right.anchor = GridBagConstraints.EAST; right.weightx = 2.0; right.fill = GridBagConstraints.HORIZONTAL; right.gridwidth = GridBagConstraints.REMAINDER; frame.getContentPane().add(BorderLayout.NORTH, noValid); frame.getContentPane().add(BorderLayout.CENTER, panel); panel.add(userTxt, left); panel.add(createUsername, right); panel.add(userPass, left); panel.add(createPassword, right); panel.add(confirmPass, left); panel.add(confirmPassword, right); frame.getContentPane().add(BorderLayout.SOUTH, createAccount); frame.setVisible(true); frame.setSize(500, 300); createAccount.addActionListener(this); } public void actionPerformed(ActionEvent event) { if (createUsername.getText().length() &lt;= 0 ) { noValid.setText("That is not a valid username. Please try again."); frame.getContentPane().add(BorderLayout.NORTH, noValid); } else if (allUsernames.contains(createUsername.getText())) { noValid.setText("That username is already taken. Please try again."); frame.getContentPane().add(BorderLayout.NORTH, noValid); } //THIS IS THE PART I'M CONFUSED ABOUT else if (!(createPassword.getPassword().equals(confirmPassword.getPassword()))) { noValid.setText("Your passwords do not match!"); frame.getContentPane().add(BorderLayout.NORTH, noValid); } else { SaveUser sUser = new SaveUser(); sUser.createAccount(this); noValid.setText("Account created successfully"); frame.getContentPane().add(BorderLayout.NORTH, noValid); } } } </code></pre> <p>And the serializing class: </p> <pre><code>package passwordProgram; import java.io.FileOutputStream; import java.io.ObjectOutputStream; public class SaveUser { public void createAccount(User u) { try { FileOutputStream fileOS = new FileOutputStream("userInfo.txt"); ObjectOutputStream objectOS = new ObjectOutputStream(fileOS); objectOS.writeObject(u); objectOS.close(); } catch (Exception e) { e.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload