Sajad Sajad - 1 year ago 57
Java Question

Login jframe password control with text file

I have a login frame, That has two different login mode, "User" and "Admin".

My problem is in admin mode (when select admin from jcombobox),

When i select admin, my first textfield should fill with "Administration" automatically and did, And in my jpasswordfiled, It should search it's password number from text file (that is 2).

But, Not accept in admin mode:

public class LoginFrame extends javax.swing.JFrame implements ActionListener {

private String username;
private char[] Password;

private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {

username = String.valueOf(jTextField1.getText());
Password = jPasswordField1.getPassword();

if (jComboBox1.getSelectedIndex() == 2) {
if (adminCanGoNext2()) {
goAdminMainPage(); // Execute work
} else {
ErrorMessageLabel.setText("Did Not Match");
} catch (Exception e) {
ErrorMessageLabel.setText("Enter Correct Input");

public boolean adminCanGoNext2() throws IOException {
FileReader fr = new FileReader("LoginInformation.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while( (line = br.readLine())!= null ){
char[] charedPass=line.toCharArray(); // char password that read from file
System.out.println("readed password is: "+ charedPass.toString());
if(Arrays.equals(charedPass, Password)){
return true;
return false;

public void goAdminMainPage() {
System.out.println("Go ");

public void actionPerformed(ActionEvent e) {
if (e.getSource() == jComboBox1) {
if (jComboBox1.getSelectedIndex() == 2) {

Login Information.txt file:

Admin 2
271 tes tt Male 2013/05/30
458 tttt uuuu Female 2013/05/30

Now, When i select admin mode, my jtextfield1 text is "Administration" perfectly,

But when i try number "2" for passwordfield, and clicked to login button, make no change!

Answer Source

You are comparing a String with a character array (and they are always found non-equal obviously).

You have to convert the password read from the file into a character array using toCharArray() and then compare the resulting array with the character array returned by JPasswordFields gePassword() method. For easy comparing of arrays you can use the utility methods of java.util.Arrays.

See, also, this short demo.

Please note, that storing passwords in String variables is not advisable due to security concerns. (Of course storing passwords in files in clear-text is probably an even greater security risk.) The "How to Use Password Fields" section of the Java Tutorials might be a good source of info and directions to get you started.