Array linear search: out of bounds

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geebanga88
    New Member
    • Aug 2007
    • 62

    #1

    Array linear search: out of bounds

    HI i have a method that performs a linear search to find a given vowel. The vowel parameter is a given vowel, while vowel[] is the array with all the vowels.

    Code:
     
    
     public static void DisplayFirstAppearance (char vowel, char vowels[]) {
           boolean element_found = false;
            int index;
            
            for (index = 0; element_found == false && index < NoVowels; ++index); { 
               
               if (vowels[index] == vowel)  { // Error Here
                       element_found = true; 
                        }
            if (element_found) {
                System.out.println("The vowel: " + vowel); 
                System.out.println("First appears: " + vowels[index]); 
            } 
            else 
                System.out.print(+ vowel); 
               System.out.println(" Vowel not found");
        }
    After running the program i get the error:
    Exception in thread "main" java.lang.Array IndexOutOfBound sException: 3
    at Main.DisplayFir stAppearance(Ma in.java:78) --> Line 7
    3 being the number of values in the array.
    Anyone know how to ovr come this i noe its somthing to do with the value not being with range or somthing.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    I don't want to disappoint you but have a look at the String.indexOf( ) method.

    kind regards,

    Jos

    Comment

    Working...