Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Norm

Thread: ArrayIndexOutOfBounds Error, Any help would be appreciated.

  1. #1
    Jtrtoday is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default 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);
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.
    Jtrtoday likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Jtrtoday is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: ArrayIndexOutOfBounds Error, Any help would be appreciated.

    Quote Originally Posted by Jtrtoday View Post
    Thank you very much!!! on line 58 I just changed it to: return get_leftish(s, s.length-1); the -1 fixed it all.
    I don't think so; your last method doesn't check the word in the array at position #0.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. problem with ArrayIndexoutofbounds exception
    By hyma19 in forum New To Java
    Replies: 1
    Last Post: 01-16-2012, 10:13 AM
  2. Replies: 4
    Last Post: 04-14-2011, 07:45 AM
  3. ArrayIndexOutOfBounds, JTable.getValueAt
    By ZackO in forum AWT / Swing
    Replies: 10
    Last Post: 10-17-2010, 07:29 PM
  4. ArrayIndexOutofBounds Exception
    By atul.goldenstring in forum New To Java
    Replies: 10
    Last Post: 04-10-2010, 10:47 AM
  5. ArrayIndexOutOfBounds
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-07-2009, 12:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •