Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get index of ArrayList

 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15492
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thank's.

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

thank's in advance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic