Results 1 to 4 of 4
- 04-09-2012, 03:59 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 15
- Rep Power
- 0
ArrayIndexOutOfBounds Error, Any help would be appreciated.
All the code works fine, except in last method. That seems to be where the error is coming from. Any help would be appreciated.
The error I'm getting is Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
Java Code:public class Wordish { public static void main(String[] args) { System.out.println("testing elfish:"); System.out.printf("elfish(\"whiteleaf\") is %b.\n", elfish("whiteleaf")); System.out.printf("elfish(\"Java\") is %b.\n\n", elfish("Java")); System.out.println("testing x_ish:"); System.out.printf("x_ish(\"left\", \"rightfullness\") is %b.\n", x_ish("left", "rightfullness")); System.out.printf("x_ish(\"left\", \"shelf\") is %b.\n\n", x_ish("left", "shelf")); String[] s = {"this", "stressful", "time", "on", "the","twelfth", "felt", "strangely", "uneventful"}; System.out.println("testing keep_leftish:"); System.out.printf("keep_leftish(s) returns %s.\n", keep_leftish(s)); } // a normal method to check whether word contains the letters of e, l, and f. public static boolean elfish(String word) { String elf = "elf"; int Total =0; for(int count=0;count<elf.length();count++){ if (word.indexOf(elf.substring(count,count+1)) != -1){ Total +=1; } } if (Total==elf.length()) return true; else return false; } // checking whether word contains all the letters of a given word x public static boolean x_ish(String x, String word) { return x_ish_check(x, x.length(), word); } // a recursive method to check whether word contains // all the first n letters of a given word x private static boolean x_ish_check(String x, int n, String word) { if (n <1) return true; else if(word.indexOf(x.substring(n-1,n)) != -1){ return x_ish_check(x.substring(0,n-1),n-1, word); } else return false; } // This method finds all the leftish words in the array s // and returns a string of all the found words concatenated. public static String keep_leftish(String[] s) { return get_leftish(s, s.length); } // recursive method to find all the left like words among the first n words of array s // and return a string of all the found words concatenated. private static String get_leftish(String[] s, int n) { boolean True = true; String word = s[n]; if (n==0) return ""; else if(True == x_ish("left",word )) return s[n] + get_leftish(s,n-1); else return "" + get_leftish(s,n-1); } }
- 04-09-2012, 04:33 PM #2
Re: ArrayIndexOutOfBounds Error, Any help would be appreciated.
Make sure the value of an array index is in bounds before using it. Remember that valid index values are from 0 to the array's length -1.
If you don't understand my response, don't ignore it, ask a question.
- 04-09-2012, 04:42 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 15
- Rep Power
- 0
Re: ArrayIndexOutOfBounds Error, Any help would be appreciated.
Thank you very much!!! on line 58 I just changed it to: return get_leftish(s, s.length-1); the -1 fixed it all.
- 04-09-2012, 05:30 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
problem with ArrayIndexoutofbounds exception
By hyma19 in forum New To JavaReplies: 1Last Post: 01-16-2012, 11:13 AM -
index not reaching a value, still ArrayIndexOutOfBounds exception at that value
By Navin Israni in forum New To JavaReplies: 4Last Post: 04-14-2011, 08:45 AM -
ArrayIndexOutOfBounds, JTable.getValueAt
By ZackO in forum AWT / SwingReplies: 10Last Post: 10-17-2010, 08:29 PM -
ArrayIndexOutofBounds Exception
By atul.goldenstring in forum New To JavaReplies: 10Last Post: 04-10-2010, 11:47 AM -
ArrayIndexOutOfBounds
By SwEeTAcTioN in forum New To JavaReplies: 6Last Post: 12-07-2009, 01:59 AM
Bookmarks