Results 1 to 6 of 6
Thread: comparing arrays..
- 05-24-2009, 11:26 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 14
- Rep Power
- 0
comparing arrays..
ok, here's my code:
Java Code:import javax.swing.*; public class LogIn { private String [] UserName = {"Cloud", "Tifa", "Barret"}; private String [] UserPassword = {"Strife", "Lockheart", "Wallace"}; private String Name; private String Password; private int iCount = 0; private int i; public void SetUserName() { boolean found = false; while (!found) { Name = JOptionPane.showInputDialog("Please enter your user ID"); for (int x = 0; x < UserName.length; x++) { if (Name.equals(UserName[x])) { found = true; i = x; JOptionPane.showMessageDialog(null, "Thank you."); SetPassword(); break; } } } /* do { Name = JOptionPane.showInputDialog("Please enter your user ID"); iCount++; for(int i = 0;i < UserName.length;i++) { if (Name.equals(UserName[i])) { JOptionPane.showMessageDialog(null, "Thank you."); SetPassword(); } } }while(!found && iCount != 3); */ } public void SetPassword() { boolean found = false; while (!found) { Password = JOptionPane.showInputDialog("Please enter your user password"); for (int x = 0; x < UserPassword.length; x++) { if (Password.equals(UserPassword[x])) { found = true; i = x; JOptionPane.showMessageDialog(null, "Thank you for logging in "); } { found = true; i = x; break; } } } /* do { Password = JOptionPane.showInputDialog("Please enter your user password"); iCount++; for(int i = 0;i < UserPassword.length;i++) { if (Password.equals(UserPassword[i])) { //found = true; JOptionPane.showMessageDialog(null, "Thank you. LogIn comeplete"); System.exit(0); } } }while(!found && iCount != 3); */ } public static void main(String[] args) { LogIn oRun = new LogIn(); oRun.SetUserName(); } }
thanks,
CP
- 05-24-2009, 11:31 PM #2
You need some kind of Map from usernames to (preferably secure hashes of) passwords.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-24-2009, 11:39 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 14
- Rep Power
- 0
that made no sense to me...I'm a simpleton. secure hashes of passwords?
- 05-25-2009, 01:52 AM #4
Cryptographic hash function - Wikipedia, the free encyclopedia available in java.security (called a digest)
Maps available in java.utilDon't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-25-2009, 05:16 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
Your code is looping through every password in the password array when the user submits a password meaning it will accept any of the passwords you have in your password array. What you are doing makes no sense, I don't know what version of Java you are using but try using an enumerated HashMap like this: HashMap<String,String> :this allows you to have a String,String key value pair, this way you can iterate through the maps keys to find the username and then compare if the key's value is equal to the password. This is just one simple way. If your application is going to be used by others then you really need to consider obfuscating your passwords via an MD5 or SHA1 hash. These hashes are 1 way only meaning if someone were able to access your password list it would be useless to them. What you would do is run the saved password through the MD5 or SHA1 hash digest and save the hash of the password. Then to compare you run the input password through the same MD5 or SHA1 digest and compare the result to the saved one.
- 05-25-2009, 08:05 AM #6
Hi,
Go thru this changed version of the code in red
import javax.swing.*;
Java Code:public class LogIn { private String [] UserName = {"Cloud", "Tifa", "Barret"}; private String [] UserPassword = {"Strife", "Lockheart", "Wallace"}; private String Name; private String Password; private int iCount = 0; private int i; public void SetUserName() { boolean found = false; while (!found) { Name = JOptionPane.showInputDialog("Please enter your user ID"); for (int x = 0; x < UserName.length; x++) { if (Name.equals(UserName[x])) { found = true; i = x; JOptionPane.showMessageDialog(null, "Thank you."); SetPassword(); break; } } } /* do { Name = JOptionPane.showInputDialog("Please enter your user ID"); iCount++; for(int i = 0;i < UserName.length;i++) { if (Name.equals(UserName[i])) { JOptionPane.showMessageDialog(null, "Thank you."); SetPassword(); } } }while(!found && iCount != 3); */ } public void SetPassword() { boolean found = false; while (!found) { Password = JOptionPane.showInputDialog("Please enter your user password"); for (int x = 0; x < UserPassword.length; x++) { [COLOR="Red"][B] //Change it to i here instead of x.whatever username entered by u should have the //same index as that of UserPassword array.So iam taking the username entered index //and selcting the corresponding password //if (Password.equals(UserPassword[x])) if (Password.equals(UserPassword[i])) [/B][/COLOR] { found = true; JOptionPane.showMessageDialog(null, "Thank you for logging in "); } { found = true; i = x; break; } } } } public static void main(String[] args) { LogIn oRun = new LogIn(); oRun.SetUserName(); } }
Ramya:cool:
Similar Threads
-
Comparing two images
By GhosT in forum Advanced JavaReplies: 13Last Post: 04-25-2009, 01:37 AM -
Comparing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 10:44 AM -
comparing
By Feng in forum New To JavaReplies: 2Last Post: 11-23-2007, 10:40 AM -
Comparing JavaWebFrameworks
By pegitha in forum Web FrameworksReplies: 1Last Post: 05-18-2007, 07:23 PM
Bookmarks