• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to get index of ArrayList

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello every one.

i have two ArayList - user and password and fill with this code :


List user = new ArrayList();
List password = new ArrayList();


while ( rs.next() ) {
user.add(rs.getString("user_nick"));
password.add(rs.getString("user_password"));

}


and i try with :

Iterator i = user.iterator();
while (i.hasNext()) {
if (i.next().equals("admin")){ ??? );}
}

and i wont to check the same index of user that the value is "admin" with the same index in password ArrayList to have validation for user and password entry.

Thank's
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try looping through the ArrayList by index instead of using an Iterator:
 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i try


for (int i = 0; i < user.size(); i++) {
if ((user.get(i).equals("admin")) || (password.get(i).equals("1")) ) {
out.println("you a admin");
};

but i working with in servlet and the server side return this error :

java.lang.NullPointerException
userEntryy.checkUsers.processRequest(checkUsers.java:66)
userEntryy.checkUsers.doGet(checkUsers.java:118)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i try only this

for (int i = 0; i < user.size(); i++) {
if ((user.get(i).equals("admin"))) {
out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");

}

it's working fine. What's up here. Where is the proglem.

Thank's
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try looking at the Javadoc for ArrayList for a method that might give you the index Of an object ?

Of course, a better solution would be to create a class to hold your username and password and just add instances of this class to the ArrayList. You will need to override the equals() and hashCode() methods in your class in order to retrive the instances from the ArrayList. A bit more work, but when you come to add more attributes to your user it will make life a lot easier,
 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank's Joanne.
I'm a realy new in this proglem and i'll be happy if you can give a little example how to do that. It'll be very helpful for me to understand how the think is working.

Thank a lot.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wont it be better if you use HashMap or Hashtable with or without ArrayList, this way you will be able to store key-value pairs for user and password. Then you can get the password for a given user.
 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank's.

Can you give me some example, please. My brain is almost mishmash.

thank's in advance.
 
reply
    Bookmark Topic Watch Topic
  • New Topic