Results 1 to 4 of 4
  1. #1
    mitchell b is offline Member
    Join Date
    Jul 2016
    Posts
    1
    Rep Power
    0

    Default getPassword to String, char[] to string

    Hey guys,

    I've got a question, i'm trying to create a simple java log in page using swing and i've come across an issue. What i've got is a a JTextField and a JPasswordField and what I want to happen is when I enter a username and password I want the input to be checked against variables that have been programmed into the code. So far what I know and have worked out is the JTtextField returns a string and JPasswordField returns a char[]. I've got the JTextField part sorted out but the JPasswordField isn't working and I can't seem to see where i'm going wrong. Can someone help me out and push me in the right direction?

    Java Code:
     
    char[] c = new char[]{'t', 'e', 's', 't'};
    String passwordString = new String(c);
    Java Code:
    button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
    
                    if (userTextField.getText().equals(userString) && (passwordField.getPassword().toString()).equals(userString)){
                        frame.dispose();
                        new SecondFrame();
                    } else
                        JOptionPane.showMessageDialog(null, "Please try again, the details you entered are incorrect!");
                    System.out.println(passwordField.getPassword().toString());
                    System.out.println(userTextField.getText().equals(userString));
                }
            });

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: getPassword to String, char[] to string

    Shouldn't you compare that password against the passwordString? (not the userString).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    32

    Default Re: getPassword to String, char[] to string

    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: getPassword to String, char[] to string

    This was also on Code Ranch somewhere.

    Anyway, the issue is that getPassword returns a char[], and a toString on that gives the default (Object) value (ie <class>@<hashcode>) so it will never equal a password string.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 09-26-2013, 11:35 AM
  2. String and sum int value of char
    By bentho in forum New To Java
    Replies: 1
    Last Post: 09-20-2012, 03:24 AM
  3. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  4. Char to Bit String
    By Krooger in forum New To Java
    Replies: 2
    Last Post: 01-29-2010, 02:26 AM
  5. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 01:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •