Closed Thread
Results 1 to 5 of 5

Thread: String Trim program. Unexpected result. Array Index Out of Bound

  1. #1
    lovemarshall is offline Newbie
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Exclamation String Trim program. Unexpected result. Array Index Out of Bound

    Hi, This code produce an array out of index error. Can any one help please?

    HTML Code:
    /**
     * @(#)TrimEndSpaces.java
     *  Program to trim any space at the end of a string
     *
     * @author Marshall
     * @version 1.00 2010/3/10
     */
    public class TrimEndSpaces {
    	static String trim(String s)
    	{
    		char chr[] = s.toCharArray();
    		int i=0;
    		int length = chr.length;
    
    		while(chr[length]==' ')
    		{
    				length--;
    		}
    
     		char trimmed[] = new char[length];    //Define and Store the new trimmed string in char array
    		for(i=0;i<length;i++)
    		{
    			trimmed[i]=chr[i];
    		}
    
    		String trimmedString = new String (trimmed);  // Converts the trimmed char array to string.
    		return trimmedString;
    	}
    
    	public static void main(String args[])
    	{
    		String str = "Do  ";
    		str = trim(str);
    		System.out.println(str);
    	}
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: String Trim program. Unexpected result. Array Index Out of Bound

    Same thing as in your previous post. When you convert a string to an array, the last element in array is not at index str.length()!!!!!!! It's at index (str.length()-1). Array indexes vary from 0 to (#array elements -1).

  4. #3
    Sinipull's Avatar
    Sinipull is online now Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: String Trim program. Unexpected result. Array Index Out of Bound

    try
    Code:
    while(chr[length-1]==' ')
    instead.

    Btw, why do you reinvent this method anyway? there's method trim() in the String class...

  5. #4
    lovemarshall is offline Newbie
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Re: String Trim program. Unexpected result. Array Index Out of Bound

    Hi,

    Must say thanks to you before I go back and correct the code. I was asked by my trainer to make own function for that.


    thanks again

  6. #5
    lovemarshall is offline Newbie
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Re: String Trim program. Unexpected result. Array Index Out of Bound

    Now it works like magic!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Array Index Out Of Bound
    By lovemarshall in forum Java Help
    Replies: 3
    Last Post: 03-08-2010, 12:15 PM
  2. Random generation of string, towards goal result
    By Bishop in forum General Programming
    Replies: 4
    Last Post: 03-04-2010, 02:38 AM
  3. Replies: 7
    Last Post: 01-24-2010, 10:02 PM
  4. Replies: 3
    Last Post: 08-12-2009, 08:00 AM
  5. String.Trim not working
    By dirkfirst in forum C# Programming
    Replies: 2
    Last Post: 07-24-2006, 06:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

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