Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a vector to store more then one username and

Status
Not open for further replies.

adolsun

Programmer
Jan 13, 2004
52
GB
just want to know how to create a vector to store more then one username and
password, which i can call up each time when my program is executed.

username1:admin
password: 111
username2: user
password: 222

Thanks

This is my code:
-------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class login extends JFrame
{public static void main(String[] args)
{login login=new login();
login.show();
}
TextField text1,text2,text3,text4;
JButton b1,b2;
boolean confirm=false;

public login()
{setSize(300,100);
Container windowPane=getContentPane();
windowPane.setLayout(new GridLayout(3,2));
text1=new TextField("username:");
text1.setEditable(false);
text2=new TextField(6);
text3=new TextField("password:");
text3.setEditable(false);
text4=new TextField(6);
text4.setEchoChar('*');
b1=new JButton("OK");
b2=new JButton("Quit");
windowPane.add(text1);
windowPane.add(text2);
windowPane.add(text3);
windowPane.add(text4);
windowPane.add(b1);
windowPane.add(b2);
b1.addActionListener(new ConfirmListener());
b2.addActionListener(new QuitListener());
}

private class ConfirmListener implements ActionListener
{public void actionPerformed(ActionEvent e)
{confirm=true;
}
}
private class QuitListener implements ActionListener
{public void actionPerformed(ActionEvent e)
{System.exit(0);
}
}
}
 
I would use an inner class to store the user data, and add that class to a Vector, maybe something like :

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class login extends JFrame
{public static void main(String[] args)
  {login login=new login();
   login.show();
  }
 Vector users = new Vector();
 TextField text1,text2,text3,text4;
 JButton b1,b2;
 boolean confirm=false;

 public login()
 {setSize(300,100);
  Container windowPane=getContentPane();
  windowPane.setLayout(new GridLayout(3,2));
  text1=new TextField("username:");
  text1.setEditable(false);
  text2=new TextField(6);
  text3=new TextField("password:");
  text3.setEditable(false);
  text4=new TextField(6);
  text4.setEchoChar('*');
  b1=new JButton("OK");
  b2=new JButton("Quit");
  windowPane.add(text1);
  windowPane.add(text2);
  windowPane.add(text3);
  windowPane.add(text4);
  windowPane.add(b1);
  windowPane.add(b2);
  b1.addActionListener(new ConfirmListener());
  b2.addActionListener(new QuitListener());
 }

  private class ConfirmListener implements ActionListener {
   public void actionPerformed(ActionEvent e) {
   	confirm=true;
   	User user = new User();
   	user.user = text1.getText();
   	user.password = text3.getText();
   	users.add(user);
    }
   }
  private class QuitListener implements ActionListener
   {public void actionPerformed(ActionEvent e)
     {System.exit(0);
     }
   }
   
   class User {
      String user = "";
      String password = "";
      
   }
}
 
I gave it a try as it's but I received an error with this line:

Vector users = new Vector();

Thanks in advance
 
You must add import the Vector class :

import java.util.Vector;
 
Yes, thanks, it's working. Please if you have time can you tell me where (or how) to add:
username1:admin
password: 111
username2: user
password: 222
username3: user
password: 333

and then the suitable next interface will be displayed:
adminMenu for Admin and userMenu for user.

Thanks
 
I'm not sure about what you are asking, but, if you want to get the data from the Vector, apply this code to your application :
Code:
for (int i = 0; i < users.size; i++) {
 User u = (User)users.get(i);
 String uName = u.user;
 String uPwd = u.password;
 System.err.println(uName +" " +uPwd);
}
 
thanks sedj, I mean when you log in you must enter only one of the folllowing usernames and passwords in order to get to the next interface (menues):

username1:admin
password: 111
username2: user
password: 222
username3: user
password: 333

Thanks
 
At creation time, fill the Vector with usernames and passwords. Then you want to check it against the Vector.

Code:
Vector a = new Vector();
User u = new User("admin","111");
a.add(u);

...

User b = new User();
b.setName(Text1.getText());
b.setPw(Text3.getText());
for(int k = 0; k < a.size(); a++) {
  if((User)a.getElementAt(k).equals(b)) {
    //Voila! We have a match!
  }
}
Overide the equals method in User.
 
When you use a Map instead of a Vector, then you don't need to create a class "User". You can use the 'username' as the key and the 'password' as the value. You also don't need to code a loop to check/retrieve a password, you can just call "get(key)" or "containsKey(key)" ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top