chief
chief

Reputation: 3

basic search through array

Name = Input.next();

for(int K = 0; K < CurrentCount ; K++) {
  if( ArrayList[K].LastNamePlayer == Name)
    System.out.println(
      "Name " + ArrayList[K].GetName() + ArrayList[K].GetLastName());

I can access all the values in the array but when I set a condition it returns nothing when it should.

Upvotes: 0

Views: 437

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1503290

You're comparing strings by reference. In other words, you're checking whether the references ArrayList[K].LastNamePlayer and Name are references to the exact same object. I suspect you want:

if (ArrayList[K].LastNamePlayer.equals(Name))

which will check for string equality... whether they're references to equal strings: ones which represent the same sequence of characters.

Note that these variable names are unconventional for Java - normally you'd have name, lastNamePlayer etc, using camel case instead of Pascal case. Similarly the method name would normally be getName() instead of GetName().

Also, it looks like ArrayList is an array, not an ArrayList, so it's a pretty misleading name. It would be more useful if it described what it represented rather than the storage, too.

Upvotes: 2

Ocaso Protal
Ocaso Protal

Reputation: 20267

Instead of

if( ArrayList[K].LastNamePlayer == Name)

write

if( ArrayList[K].LastNamePlayer.equals(Name))

Upvotes: 1

Related Questions