-1

I am supposed to write sequential/linear search on a String array. I am very close to finishing, but part of the assignment confuses me. It says to compare the target item with successive element of the list until the target matches or target is less than the current element of the array. How can a String be more or less than another element when there is no numerical value? Maybe I'm just not thinking about it correctly. Here is my program so far:

public class SequentialSearchString {
public static boolean sequential (String[] numbers){
    //Set the target item to an arbitrary String that should return true.
    String T1 = "Frank";  

    for (int i = 0; i < numbers.length; i++){
        if (numbers[i] == T1){
            return true;    
        }
        if (numbers[i] != T1){
            numbers[i] = numbers[i+1];
        }           
    }
    return false;   
}

public static boolean sequential2 (String[] numbers){
    //Set the target key to String that should return false.
    String T2 = "Ian";
    for (int i = 0; i < numbers.length; i++){
        if (numbers[i] == T2){
            return true;    
        }
        if (numbers[i] != T2){
            numbers[i] = numbers[i+1];
        }
    }
    return false;   
}


public static void main(String[] args) {
    //Create a list of 8 Strings.
    String [] numbers = 
{"Ada", "Ben", "Carol", "Dave", "Ed", "Frank", "Gerri", "Helen", "Iggy", "Joan"};   
    //If the first target item (T1) is found, return Succuss. If not, return failure.
        if (sequential(numbers) == true){
            System.out.println("Success. 'T1' was found");
        }
        else {
            System.out.println("Failure. 'T1' was not found");  
        }
    //If the second target item (T2) is found, return Succuss. If not, return failure.
        if (sequential2(numbers) == true){
            System.out.println("Success. 'T2' was found");
        }
        else {
            System.out.println("Failure. 'T2' was not found");  
        }   
    }   
}

The first method works fine, but I appear to be having issues with searching for elements that are not in the list. Here is the error message I get after running the program:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at SequentialSearchString.sequential2(SequentialSearchString.java:32)
at SequentialSearchString.main(SequentialSearchString.java:50)
Success. 'T1' was found

Any help understanding the assignment and fixing the exception would be much appreciated.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Bryan
  • 2,951
  • 11
  • 59
  • 101
  • For info : you cannot compare String values using == and !=, it doesn't work (it only compares the "adresses" of the objects). You have to use string1.equals(string2) – huelbois Feb 15 '12 at 22:50
  • Use `String#compareTo` to compare the strings. Here's a related question that explains more: http://stackoverflow.com/questions/4064633/string-comparison-in-java . – david a. Feb 15 '12 at 22:55
  • As for the "less than" request, a Java String implements Comparable. When you call Comparable.compareTo() to compare an Object to another one, return value indicates the relative natural ordering of the Objects. In the case of Strings, the natural ordering is lexicographical. – Kevin Welker Feb 15 '12 at 23:01

2 Answers2

1
numbers[i] = numbers[i+1];

Is likely to cause your ArrayIndexOutOfBoundsException.

Your clause checks i < numbers.length. So you set the bounds. However if i == numbers.length - 1 then you will try to access i+1 which is larger then your array so it is out of bounds.

For example: numbers.length is 4. So i can be 3. With i+1 you try to access numbers[4] which would be the fifth position as arrays start with 0 and numbers[3] would be the last position.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
0

The ArrayIndexOutOfBoundsException is due to the fact you use:

for (int i = 0; i < numbers.length; i++)

and latter:

 numbers[i] = numbers[i+1];

When i equals numbers.length-1 (last iteration), i+1 equals numbers.length. Then you try to read numbers[numbers.length] which is wrong (valid index is from 0 to numbers.length-1).

You've got to use :

for(int i=0;i<numbers.length-1;i++) 

to prevent the exception. Now, I'm not sure it would solve your whole problem, but the Exception certainly.

huelbois
  • 6,762
  • 1
  • 19
  • 21
  • I did what you recommended and sure enough the exception is gone and the program runs, however there is one problem. I tested each element and the first and last ("Ada" and "Joan") return a failure even though they are in fact in the list. Is there a way I can fix this? – Bryan Feb 15 '12 at 23:17