Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Getting ArrayIndexOutBound Exception

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Getting ArrayIndexOutBound Exception

    Hello I am writing the java program to get the Length of the String without String method.So I am using loop.
    But I am getting the error as -
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14
    at StringLengthwithoutMethod.main(StringLengthwithout Method.java:17)


    Here is my program -

    public class StringLengthwithoutMethod {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    String str = "This is string";
    int i=0;
    char[] chararr = str.toCharArray();

    while(chararr[i]!='\0'){
    ++i;
    }

    }
    }


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    373
    Thanks
    0
    Thanked 64 Times in 54 Posts

    Default Re: Getting ArrayIndexOutBound Exception

    Well, how about adding some debug messages to your program to see what the problem is?
    I would advice you to print the variable i, the char at i and the length of chararr in every iteration of the loop.
    Maybe this will help you to find your error by yourself.

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Getting ArrayIndexOutBound Exception

    while(chararr[i]!='\0'){
    What might happen if the string does not contain the '\0' character? In fact, does the string contain the '\0' character? Are you coming from a C++ background (String's in java are not null terminated)?

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting ArrayIndexOutBound Exception

    Yes I am from C++ background.

    So as String in java is not Null terminated.Then how we can find once we come to end of the string in java.Becuase here we can not use normal string lenght() method.

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Getting ArrayIndexOutBound Exception

    Then how we can find once we come to end of the string in java.Becuase here we can not use normal string lenght() method.
    Why can't you use the String length() method? You can also access the length of the returned char array using the build in length property of the array. (See
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics) )

Similar Threads

  1. Replies: 1
    Last Post: May 25th, 2014, 05:36 AM
  2. Replies: 4
    Last Post: February 20th, 2013, 09:01 AM
  3. Replies: 1
    Last Post: February 6th, 2013, 10:21 AM
  4. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  5. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM