0

So I've been trying to figure this out on my own for the past couple of hours but I'm stuck.

I have an array that has a list of a person's name, age, height (in cm). I want to create a method where I use only the person's name as a parameter and searches for the name in the array; if there is no matching name, return null.

Looks like this:

data = new data[50];
data[0] = new data("Dan", 23, 179);
data[1] = new data("David", 20, 180);
data[2] = new data("Sharon", 19, 162);
data[3] = new data("Jessica", 22, 160);
data[4] = new data("Nancy", 25, 164);
...
numberData = 30; // this is the number of people that are in this array so far

This is what I've been trying so far..

public data findData(String name) {
   for (int i = 0; i < numberData; i++) {
        if (name == data[i]) {
            return data[i];
        } else {
            return null;
        }
    }
}

I know it isn't right, but I can't seem to find a solution. Any ideas?

codingishard
  • 41
  • 1
  • 4
  • name.equals(data[i]) - Reference to: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Tizianoreica Dec 05 '16 at 10:07
  • 2
    Please show your `data` class. Also you would usually call classes with an upper-case first letter. Finally, it is pretty misleading when you have an array called `data`, along with a class called `data`. – Arnaud Dec 05 '16 at 10:08
  • 1
    You should use the property `length` of the arrays instead or using your `numberData` variable. – AntoineB Dec 05 '16 at 10:12

7 Answers7

1

array is referencing the Data class with name parameter so we should compare with name parameter not directly with reference of data and for string comparisons always go for equals() method.

public Data findData(String name) {
    for (int i = 0; i < numberData; i++) {
        if (name.equals(data[i].getName())) {
          return data[i];
        } 
    }
    return null;
}
Community
  • 1
  • 1
snofty
  • 70
  • 7
0
  1. In case the loop doesn't execute at least once, you're missing return value.
  2. == compares references, equals compares Strings.
  3. Return null just in case, there is no such element in the array.
  4. Class names should start with a capital letter. Please write Data instead of data.

Code:

public Data findData(String name) {
   for (Data d : data) {
        if (name.equals(d.getName())) {
            return d;
        } 
   }
   return null;
}

is what you're looking for. In the original code, null was returned if the 0th element wasn't name.

OK, the above was a quick fix and now some theory:

The pseudo code for linear search in an array:

  • Loop through all elements in an array. If any matches with the one you're looking for, return it.
  • If nothing was returned, return the indicating value (null in our case).

Look, in the original code, on the 0th element, you decided whether to return that element or a null. Also, if the loop wasn't run at least once, there was no return statement to hit.

xenteros
  • 15,586
  • 12
  • 56
  • 91
0

Use equals() to compare strings,
e.g.

if(name.equals(data[i].getName())) {
    statements...
}
Nirav Madariya
  • 1,470
  • 2
  • 24
  • 37
0

Since you want to compare strings you must use the equals method.

Here's an example of how can you use java 8:

  public Data findData(Data[] datas, String name) {
    return Arrays.stream(datas).filter(data -> data.getName().equals(name)).findAny().orElse(null);
}
-1

You should use equals() to compare strings. equals() checks the actual contents of the string, == checks if the object references are equal.

And also, as mentioned above, move return null outside the loop;

Piotr Sołtysiak
  • 998
  • 5
  • 13
-1

You can use following code. Assuming that your data class will have getName() method which returns the name value.

public data findData(String name) {
 for (int i = 0; i < numberData; i++) {
     if (name.equals(data[i].getName())) {
         return data[i];
     }
 }
 return null;
}
Coder
  • 1,917
  • 3
  • 17
  • 33
-2

move the return null statement out of the loop. Oh! and yes, use the equals() method instead of ==

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24