467,925 Members | 1,912 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,925 developers. It's quick & easy.

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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  public static void DisplayFirstAppearance (char vowel, char vowels[]) {
  4.        boolean element_found = false;
  5.         int index;
  6.  
  7.         for (index = 0; element_found == false && index < NoVowels; ++index); { 
  8.  
  9.            if (vowels[index] == vowel)  { // Error Here
  10.                    element_found = true; 
  11.                     }
  12.         if (element_found) {
  13.             System.out.println("The vowel: " + vowel); 
  14.             System.out.println("First appears: " + vowels[index]); 
  15.         } 
  16.         else 
  17.             System.out.print(+ vowel); 
  18.            System.out.println(" Vowel not found");
  19.     } 
  20.  
After running the program i get the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Main.DisplayFirstAppearance(Main.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.
Oct 14 '07 #1
  • viewed: 1471
Share:
1 Reply
Expert 8TB
I don't want to disappoint you but have a look at the String.indexOf() method.

kind regards,

Jos
Oct 14 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.