Results 1 to 6 of 6
  1. #1
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default 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();
        }
    }
    I've changed some things around. The do....while loops that I had in there I have commented out to try it this way, and it works, HOWEVER, it does not recognize the patterns of the arrays. What I mean by this is that if I enter "Cloud" as my username, it accepts any of the passwords, which is not what it is supposed to be doing (obviously). I would like it to be username and password required so when I enter "Cloud" it should only accept "Strife" as a password. I'm not entirely sure how to do this. can someone make a suggestion?

    thanks,
    CP

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  3. #3
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    that made no sense to me...I'm a simpleton. secure hashes of passwords?

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Cryptographic hash function - Wikipedia, the free encyclopedia available in java.security (called a digest)

    Maps available in java.util
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    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.

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    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

  1. Comparing two images
    By GhosT in forum Advanced Java
    Replies: 13
    Last Post: 04-25-2009, 12:37 AM
  2. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM
  3. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 09:40 AM
  4. Comparing JavaWebFrameworks
    By pegitha in forum Web Frameworks
    Replies: 1
    Last Post: 05-18-2007, 06:23 PM

Posting Permissions

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