Search code examples
javaarraysjunitlinear-search

Linear Search for Number via Person's Name Using Array - method not working if name does not exist


I am trying to implement linear search using arrays. It's supposed to search for a person's name, returning the respective number if the name exists and returning -1 if it doesn't.

I have junit tests written for 2 cases - where the name exists (testLinearSearchOK) and where it doesn't (testLinearSearchFail).

So far, I have only managed to make testLinearSearchOK pass using the following code:

   public class ASearch {


private Entry[] catalogue;
private int current;

/*
 * Assume 10 entries
 */
public ASearch(){
    catalogue = new Entry[10];
    current = 0;
}

/*
 * Ignores adding if full (should really be handled by exception...)
 */
public void addEntry(Entry e){
    if(current < 10){
        catalogue[current++] = e;
    }
}

public int linearSearch(String name){
    int current = 0;

    while (!catalogue[current].getName().equals(name))
        current++;
    return catalogue[current].getNumber();  
}
} 

The getName and getNumber methods just return the name and number respectively.

Here are the tests I've written:

@Before
public void setup(){
    as = new ASearch();
    as.addEntry(new Entry("Pete",111));
    as.addEntry(new Entry("Ken",123));
    as.addEntry(new Entry("Tim",222));
}

@Test
public void testLinearSearchOK() {
    assertEquals(123, as.linearSearch("Ken"));
}

@Test
public void testLinearSearchFail() {
    assertEquals(-1, as.linearSearch("Leo"));
}  

Any hints as to how to make this work for testLinearSearchFail as well?

Stack Trace from Eclipse (Nicholas' answer):

java.lang.NullPointerException
at ASearch.linearSearch(ASearch.java:29)
at ASearchTest.testLinearSearchFail(ASearchTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

Solution

  • Change your code to :

    public static int linearSearch(String name) {
        for (int i = 0; catalogue[i] != null; i++) {
            if (name.equals(catalogue[i].getName())) {
                return catalogue[i].getNumber();       // <--- return early if match found
            }
        }
        return -1;                                     // <--- return -1 for no match 
    }
    

    Logic:

    • If the name matches we return that number
    • If there is no match then we return a -1

    The reason your code was failing is because even when you don't find a match you try to return a number from the array catalogue.