| Author |
how to get index of ArrayList
|
Syskata Mitev
Ranch Hand
Joined: Aug 23, 2006
Posts: 51
|
|
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
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
Try looping through the ArrayList by index instead of using an Iterator:
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Syskata Mitev
Ranch Hand
Joined: Aug 23, 2006
Posts: 51
|
|
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
Joined: Aug 23, 2006
Posts: 51
|
|
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
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
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,
|
Joanne
|
 |
Syskata Mitev
Ranch Hand
Joined: Aug 23, 2006
Posts: 51
|
|
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.
|
 |
Sidd Kulk
Ranch Hand
Joined: Feb 20, 2007
Posts: 152
|
|
|
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
Joined: Aug 23, 2006
Posts: 51
|
|
thank's. Can you give me some example, please. My brain is almost mishmash. thank's in advance.
|
 |
 |
|
|
subject: how to get index of ArrayList
|
|
|