[Resolvido] Comparar dois char[]

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]

Cara, eu faria o seguinte:

char [] senha = //sua senha
char [] outraSenha = //outra senha

if(Arrays.equals(senha, outraSenha)){
//sao iguais
}
else{
//nao sao iguais
}

Para outras manipulacoes de arrays veja em:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html

t+

1 curtida

Cara usa String ao invés de char.

E para comparar utiliza equals.

Segue exemplo se tiver duvida me avisa.

public static void main(String[] args) {
		String pass1 = "senha";
		String pass2 = "senha";
		if(pass1.equals(pass2)){
			System.out.println("OK ! São Iguais !");
		} else {
			System.out.println("Senhas Diferentes !");
		}
	}
1 curtida

vlw kra, funcionou direitinho

Segue solução

[code]import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
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){
		char[] senha1 = s1.getPassword();
		char[] senha2 = s2.getPassword();
		
		if (Arrays.equals(senha1, senha2)){
			JOptionPane.showMessageDialog(null, "São iguais");
		}else{
			JOptionPane.showMessageDialog(null, "São Diferentes");
		}
	}		
}

}[/code]