Closed Thread
Results 1 to 4 of 4

Thread: Array Index Out Of Bound

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

    Question Array Index Out Of Bound

    Hi,
    I am getting an index out bound error for the following code.
    Can anybody help me please? I have also attached the java source code file.


    /**
    * @(#)ReverseString.java
    * Program to reverse a given string
    *
    * @author Marshall - Anzum.com- Hosting, SEO, Articles, Freelancing, E Learning, Downloads, How to, Online Jobs
    * @version 1.00 2010/3/8
    */

    //compiling well but giving index out of bound error
    public class ReverseString {
    static String reverse(String s)
    {
    int i,j=0;
    char str[]=s.toCharArray();
    System.out.println("Size of char array is"+str.length);
    char []newstr={};
    for(i=str.length;i>0;i--)
    {
    newstr[j]=str[i];
    j++;
    }
    String r = new String(newstr);
    return r;
    }
    public static void main(String args[])
    {
    String reversedString;
    String sentence="Reverse Me";
    reversedString=reverse(sentence);
    System.out.println(reversedString);

    }

    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

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

    Re: Array Index Out Of Bound

    Oops, all the four files appended. But we only need ReverseString.java

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

    Re: Array Index Out Of Bound

    Thats because you initiate variable 'i' in the for loop with the length of s.
    You should understand that if the length of an array is k, its indexes are 0,1,2,3,...k-1. The last index is always k-1 because you start counting from 0.
    So instead of:
    Code:
    for(i=str.length;i>0;i--)
    You should do something like:
    Code:
    int lastIndex = str.length - 1;
    for (i = lastIndex; i>=0; i--)
    ...//rest of the code
    note that the halting condition of the loop is
    Code:
    i>=0
    , because index 0 is still a legal index.
    Also the next code allocates array of length 0:
    Code:
    char []newstr={};
    on the first write to this array you also will get OutOfBounds exception.
    You should allocate an array of str.length characters:
    Code:
    newstr = new char[str.length];

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

    Re: Array Index Out Of Bound

    Thank you so much!
    I can run the program successfully now

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. using a variable as array index
    By jackson6612 in forum C and C++
    Replies: 4
    Last Post: 10-09-2011, 01:18 PM
  2. an array with char index
    By chipbu in forum C# Programming
    Replies: 2
    Last Post: 04-12-2010, 03:46 PM
  3. Replies: 4
    Last Post: 03-11-2010, 11:48 AM
  4. Replies: 7
    Last Post: 01-24-2010, 10:02 PM
  5. Using array index
    By gaylo565 in forum C# Programming
    Replies: 3
    Last Post: 05-16-2008, 09:19 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