Pessoal, eu sei que para comparar dois char fazemos assim:
[code]public static void main(String[] args) {
char car = 'a';
if (car == 'a'){
System.out.println("Certo!");
}else{
System.out.println("Errado!");
}
}[/code]
Porém, eu estou tendo problemas para comparar dois char[] . Estou usando o JPasswordField e preciso verificar se uma senha é igual à outra digitada em outro JPasswordField. Segue um esboço do que estou fazendo.
[code]import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
public class P extends JFrame implements ActionListener{
private JPasswordField s1 = new JPasswordField();
private JPasswordField s2 = new JPasswordField();
private JButton b1 = new JButton(“Ir”);
public static void main(String[] args) {
new P().exibe();
}
public void exibe(){
getContentPane().add(s1);
getContentPane().add(s2);
getContentPane().add(b1);
setLayout(null);
setSize(150, 150);
s1.setBounds(10, 10, 100, 25);
s2.setBounds(10, 45, 100, 25);
b1.setBounds(10, 80, 120, 30);
b1.addActionListener(this);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == b1){
//já tentei esses dois "if" e sempre dá senha diferente
if (s1.getPassword() != s2.getPassword()){
JOptionPane.showMessageDialog(null, "Senhas diferentes!");
}else{
JOptionPane.showMessageDialog(null, "Senhas iguais!");
}
if (!(s1.getPassword().equals(s2.getPassword()))){
JOptionPane.showMessageDialog(null, "Senhas diferentes!");
}else{
JOptionPane.showMessageDialog(null, "Senhas iguais!");
}
}
}
}[/code]