Results 1 to 4 of 4
- 05-27-2009, 07:23 PM #1
[SOLVED] [newbie] chart[].equals(char[]) ??
'm trying to check a password, however, I'm stuck trying to validate the JPasswordField.getPassword():
:confused: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++; } }
- 05-27-2009, 07:36 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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.
- 05-27-2009, 07:43 PM #3
It means 'I don't really know why'.
The output from debug statements I've written:
Output: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]); } } }
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.
- 05-27-2009, 07:50 PM #4
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
-
equals method
By timkd127 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:52 PM -
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 12:49 PM -
Does retainAll use equals ?
By Paul Richards in forum Advanced JavaReplies: 1Last Post: 10-27-2008, 08:17 PM -
Demo bar chart and pie chart
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:22 PM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-25-2008, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks