Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How do I implement a login system in java using arrays

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    18
    My Mood
    Nerdy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How do I implement a login system in java using arrays

    I did this in Visual Basic a couple of years back. What I'm doing here is creating a login system.
    I have three users and three password respectively.
    i.e. (Username) John - (Password) jMan21 , (Username) Mary - (Password) maree34 , (Username) Luke - (Password) skywalker.
    At first I used the loop function but what it would do is cycle through all the arrays (obviously) and processed dialog boxes 3 times, one saying the login is successful and the other saying the login attempt has failed.
    And I also used the "Array.equals" method.

    This is the code I used:
    package loginPackage;
     
    import java.util.*;
    import javax.swing.*;
     
    public class userInterface extends javax.swing.JFrame {
     
        String username="";
        String password="";
        String logZone;
     
     
     
     
        public userInterface() {
            initComponents();
        }
     
        private void jBtnEnterActionPerformed(java.awt.event.ActionEvent evt) {                                          
            username = jTxtFieldUser.getText();
            password = jPassFieldPassword.getText();
            logZone = (String) jCbxLogZone.getSelectedItem();
     
            String[] usernameArray={"John","Mary","Luke"};
            String[] passwordArray={"jMan21","maree34","skywalker"};
     
            if(username.equals(usernameArray) && password.equals(passwordArray))
            {
                JOptionPane.showMessageDialog(null,"Welcome "+username+"\n"+"You are logged in as: "+logZone,"Log in",JOptionPane.INFORMATION_MESSAGE);
            }
            else{
    JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
            }
        }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,266
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: How do I implement a login system in java using arrays

    if(username.equals(usernameArray) && password.equals(passwordArray))

    The above code compares a String object with an array of Strings...and will evaluate to false. You will need to loop over the array values individually. You can do something like set a boolean flag indicating whether the entered values are correct. You might also want to consider a different data structure than parallel arrays (for example a class that describes a user)

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How do I implement a login system in java using arrays

    package loginPackage;

    import java.util.*;
    import javax.swing.*;

    public class userInterface extends javax.swing.JFrame {

    String username="";
    String password="";
    String logZone;




    public userInterface() {
    initComponents();
    }

    private void jBtnEnterActionPerformed(java.awt.event.ActionEven t evt) {
    username = jTxtFieldUser.getText();
    password = jPassFieldPassword.getText();
    logZone = (String) jCbxLogZone.getSelectedItem();

    String[] usernameArray={"John","Mary","Luke"};
    String[] passwordArray={"jMan21","maree34","skywalker"};

    if((username.equals(usernameArray[i]) && password.equals(passwordArray[i])) || (username.equals(usernameArray[++i]) && password.equals(passwordArray[i]))||(username.equals(usernameArray[++i]) && password.equals(passwordArray[i])) )
    {
    JOptionPane.showMessageDialog(null,"Welcome "+username+"\n"+"You are logged in as: "+logZone,"Log in",JOptionPane.INFORMATION_MESSAGE);
    }
    else{
    JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
    }
    }

    hope this will work fine for you

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,150
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: How do I implement a login system in java using arrays

    Please don't post junk code. You'll only confuse the OP.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: How do I implement a login system in java using arrays

    cs13, please read this before you post again: The Problem with Spoon-feeding
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. What are HashCode and equals in Java and thay are implement in java?
    By diyaots in forum Java Theory & Questions
    Replies: 2
    Last Post: September 9th, 2011, 08:38 AM
  2. Source Code to Implement Circular Queue in Java
    By rainbow9 in forum Java Programming Tutorials
    Replies: 0
    Last Post: August 20th, 2011, 02:30 AM
  3. how to implement timed out event in java?!
    By migongotar in forum Java Theory & Questions
    Replies: 2
    Last Post: August 9th, 2011, 05:56 AM
  4. Login website using Java
    By cgeier in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 22nd, 2011, 11:41 AM
  5. online system on java
    By u-khan1 in forum Paid Java Projects
    Replies: 0
    Last Post: March 8th, 2011, 05:02 AM