Results 1 to 4 of 4
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    213
    Rep Power
    10

    Default [SOLVED] [newbie] chart[].equals(char[]) ??

    'm trying to check a password, however, I'm stuck trying to validate the JPasswordField.getPassword():

    Java Code:
        
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            char[] expectedPwd = {'p','a','s','s','w','o','r','d'};
    
            if (!(userName.getText().equals("joe") &&
                    (pwd.getPassword()==(expectedPwd))))
            {
                jLabel3.setText("Logon failed!");
    
                /* FIX
                 reset values to be on the safe side
                expectedPwd = null;
                pwd.setText("");
                */
    
                if (debug)
                {
                   // System.out.println("userName= " + userName.getText());
                    System.out.print("password=");
                    System.out.println(pwd.getPassword());
    
                    System.out.print("expectedPwd=");
                    for (int i = 0; i < expectedPwd.length; i++)
                    {
                         System.out.print(expectedPwd[i]);
                    }
                }
            }
        }
    
        ...
    
       [b]//this doesn't work![/b] 
        private boolean isEqual (char[] array1, char[] array2)
        {
            int countMatching = 0;
                for (int i = 0; i < array1.length; i++)
                {
                    if (array1.[i] == array2[i]) //FIX
                        countMatching++;
                }
        
        }
    :confused:

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    9

    Default

    you should post what you mean by "doesn't work", if it gives an actual error, if it's behaving a certain way and not how you expect it to, etc. i highly doubt this will even compile.

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    213
    Rep Power
    10

    Default

    It means 'I don't really know why'.

    The output from debug statements I've written:

    Java Code:
     if (debug)
                {
                   // System.out.println("userName= " + userName.getText());
                    System.out.print("password=");
                    System.out.println(pwd.getPassword());
    
                    System.out.print("expectedPwd=");
                    for (int i = 0; i < expectedPwd.length; i++)
                    {
                         System.out.print(expectedPwd[i]);
                    }
                }
            }
    Output:
    password=password
    expectedPwd=passwordBUILD SUCCESSFUL (total time: 8 minutes 24 seconds)

    That was quite a bit! :)

    It does compile, however, I've removed the part where I'm saying //doesn't work. So,

    1. Should I override the char[].equals() method, or is it overly complicated? How do I do it?
    2. How would you write a method that compares char[] values for equality?
    3. Anything else?
    Last edited by jon80; 05-27-2009 at 07:46 PM.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    11

    Default

    Java Code:
    public class Test {
        public static void main(String[] args) {
            String password = "password";
            String testStr = "password";
            System.out.println(" equals: " + password.equals(testStr));
            System.out.println("isEqual: " + test(password, testStr));
        }
    
        private static boolean test(String s1, String s2) {
            return isEqual(s1.toCharArray(), s2.toCharArray());
        }
    
        private static boolean isEqual(char[] c1, char[] c2) {
            if(c1.length != c2.length) {
                return false;
            }
            for(int i = 0; i < c1.length; i++) {
                if(c1[i] != c2[i]) {
                    return false;
                }
            }
            return true;
        }
    }

Similar Threads

  1. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 07:52 PM
  2. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 01:49 PM
  3. Does retainAll use equals ?
    By Paul Richards in forum Advanced Java
    Replies: 1
    Last Post: 10-27-2008, 09:17 PM
  4. Demo bar chart and pie chart
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:22 PM
  5. Replies: 0
    Last Post: 01-26-2008, 12:13 AM

Posting Permissions

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