Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Vector searching

Hello,

I would like some help on Vector searching....

Suppose I want to store several usernames and their corresponding passwords in a Vector, hence I create a class for these two:

public class details
{
public String username;
public String password;
}

Then I instantiate this class inside a class which contains a Vector in order to store new users and their password:

import java.util.Vector;
public class vector
{
public void vector()
{
Vector v = new Vector();
details d = new details();
d.username = "peter";
d.password = "1234";
v.addElement(d);
d.username = "tony";
d.password = "abcd";
v.addElement(d);
}
}

Is this a correct way to do it?

What about if the user enters his username, and I want to search if it exists, I have used the following code but it didn't work since the method "contains" cannot take a String parameter:

if (v.contains(new String("peter"))) System.out.println("Correct username...");

What's the correct way to search for a String in a Vector?

Thanks.
[1238 byte] By [Dreamer] at [2007-11-11 8:14:35]
# 1 Re: Vector searching
Your class has very lousy design. Look at the following class:
class User {
private String name;
private String password;

public User(String name, String password) {
setName(name);
setPassword(password);
}

public void setName(String name) {
this.name = name;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public String getPassword() {
return password;
}

@Override public boolean equals(Object o) {
if (o instanceof User) {
return ((User) o).name.equalsIgnoreCase(name);
}
return false;
}
}
Note that I override the equals(Object) method to check if the names are equal. This is because the contains(Object) method in the Collection interface iterates through the collection and checks to see if it contains the Object with the equals(Object) method.

I would also not recommend using Vectors, but ArrayLists. With the class I just wrote, you can now do this.
import java.util.*;

public class Example {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();

userList.add(new User("destin", "abcdefg"));
userList.add(new User("Dreamer", "dreamer's password"));

System.out.println(userList.contains(new User("destin", ""))); // prints true
System.out.println(userList.contains(new User("dREAMer", ""))); // prints true
System.out.println(userList.contains(new User("dddd", ""))); // prints false
}
}

Hope this helps!
destin at 2007-11-11 22:36:05 >
# 2 Re: Vector searching
This would work fine destin, however it is a little bit 'advanced' for my level, especially the overriding part......and besides, we have to use Vectors.... :cool:

So I thought of amending it like this:

import java.util.*;

class User {
private String name;
private String password;

public User(String name, String password) {
this.name = name;
this.password = password;
}

public boolean equals(Object o) {
if (o instanceof User) {
return ((User) o).name.equalsIgnoreCase(name);
}
return false;
}
}

class Example {
public static void main(String[] args) {
Vector userList = new Vector();

userList.addElement(new User("destin", "abcdefg"));
userList.addElement(new User("Dreamer", "dreamer's password"));

System.out.println(userList.contains(new User("destin", ""))); // prints true
System.out.println(userList.contains(new User("dREAMer", ""))); // prints true
System.out.println(userList.contains(new User("dddd", ""))); // prints false
}
}

I would like to remove the overriding 'equals' method, but it will obviously always return false. Is it possible to search the Vector in a more simple way? :confused:

Thanks for your help.
Dreamer at 2007-11-11 22:36:59 >
# 3 Re: Vector searching
I would like to remove the overriding 'equals' method, but it will obviously always return false.
Yes, because by default the equals method checks to see if the two objects share memory.

Is it possible to search the Vector in a more simple way?
Using the contains method? No.

You can, however, write your own method.
public boolean containsUser(Collection c, String name) {
Iterator it = c.iterator();

while (it.hasNext()) {
if (!(it.next() instanceof User)) {
continue;
}
User currentUser = (User) it.next();
if (currentUser.getName().equals(name)) {
return true;
}
}
return false;
}
You'll need to put the getName() method back in the User class though... I don't know why you took it out; it's not complex at all.
destin at 2007-11-11 22:38:08 >
# 4 Re: Vector searching
Thx destin, I will stick to your first example. Thanks again for your help.
Dreamer at 2007-11-11 22:39:08 >
# 5 Re: Vector searching
Hello destin,

Sorry to bother you again, but I can't understand why in the following part, it returns true for the first two 'println' statements even though the password wasn't passed as parameter. I would like the program to return true only if both the username and password are correct. I've tried putting a correct username and a wrong password, but it still returns true...

import java.util.*;

public class Example {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();

userList.add(new User("destin", "abcdefg"));
userList.add(new User("Dreamer", "dreamer's password"));

System.out.println(userList.contains(new User("destin", ""))); // prints true
System.out.println(userList.contains(new User("dREAMer", ""))); // prints true
System.out.println(userList.contains(new User("dddd", ""))); // prints false
}
}
Dreamer at 2007-11-11 22:40:07 >
# 6 Re: Vector searching
Hello destin,

Sorry to bother you again, but I can't understand why in the following part, it returns true for the first two 'println' statements even though the password wasn't passed as parameter. I would like the program to return true only if both the username and password are correct. I've tried putting a correct username and a wrong password, but it still returns true...

import java.util.*;

public class Example {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();

userList.add(new User("destin", "abcdefg"));
userList.add(new User("Dreamer", "dreamer's password"));

System.out.println(userList.contains(new User("destin", ""))); // prints true
System.out.println(userList.contains(new User("dREAMer", ""))); // prints true
System.out.println(userList.contains(new User("dddd", ""))); // prints false
}
}

Well, in the OP you wanted it to be true if the names were correct; you never mentioned anything about passwords.

If you look at the equals(Object) method I wrote, you'll see it returns true if the two names are equal:
@Override public boolean equals(Object o) {
if (o instanceof User) {
return ((User) o).name.equalsIgnoreCase(name);
}
return false;
}
All you have to do is make it return true of both name and password are equal:
@Override public boolean equals(Object o) {
if (o instanceof User) {
User u = (User) o;
return (u.name.equalsIgnoreCase(name)) && (u.password.equals(password));
}
return false;
}

Hope this helps!
destin at 2007-11-11 22:41:11 >
# 7 Re: Vector searching
Ah I see! Thanks!
Dreamer at 2007-11-11 22:42:09 >
# 8 Re: Vector searching
Hello,

Is it possible to create an overloading 'equals' method to check just the 'username'? So we will end up with a method to check just the 'username', and the overloading method checks both the 'username' and 'password'.

Thanks.
Dreamer at 2007-11-11 22:43:07 >
# 9 Re: Vector searching
Hello,

Is it possible to create an overloading 'equals' method to check just the 'username'? So we will end up with a method to check just the 'username', and the overloading method checks both the 'username' and 'password'.

Thanks.
You should probably use the contains user method I posted before.
destin at 2007-11-11 22:44:15 >