私は登録フォームを使ってプロジェクトを進めています。このフォームでは、ユーザーに というパスワードを入力して、JPasswordフィールドにパスワードを入力し、もう一度別のJPasswordフィールドにパスワードを入力するよう求めます。javaで2つのパスワードを比較するには?
私はJOptionPaneを使用して、パスワードが一致しない場合にユーザーにプロンプトを出します。しかし、私がpasswordField.getPassword()。toString()を2つ使用すると、それらは一致しません。 私は基本的な "12345"の両方を入力しようとしましたが、まだ運がありません。
私はあなたが.equals()を使うべきであることを知っていますが、 "!="演算子を使わずに "等しくない"のものに相当するものは何ですか?
ここには次のコードがあります。
if(e.getSource() == submit)
{
String name = nameTextField.getText();
String address = addressTextField.getText();
String phoneNumber = numberTextField.getText();
String dob = datePicker.getDateFormatString();
String password = passwordField.getPassword().toString();
String password2 = passwordFieldTwo.getPassword().toString();
try
{
//If the user leaves the field empty
if(name == null || address == null || phoneNumber == null || dob == null
|| password == null || password2 == null)
{
//Error message appears to prompt the user to
//complete the form
JOptionPane.showMessageDialog(null, "All fields must be complete to submit.", "Woops", JOptionPane.ERROR_MESSAGE);
}
if(password != password2)
{
JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);
passwordField.setText(null);
passwordFieldTwo.setText(null);
}
これについてのお手伝いをさせていただきます。
'PASSWORDFIELD .getPassword() 'は文字配列を返します.calling' toString() 'は文字列としてパスワードを与えません。 'new String();'で文字列に変換することはできますが、 'String'ではなく' char array'として返す理由がありません。/8881291/why-is-char-preferred-over-passwords-in-java –
質問が重複としてマークされた後の編集について:** '.equals 'の結果に'! ' () 'と同様の動作を得るために'!= '。** – Matt