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 7 of 7

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
    397
    Thanks
    0
    Thanked 67 Times in 56 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,235
    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,235
    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) )

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting ArrayIndexOutBound Exception

    Quote Originally Posted by nike View Post
    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.
    Since the exercise is to not use String methods for getting the length, and we already have an array of chars, we can let the array tell us when it's done using for-each loops:

    public class StringLengthwithoutMethod {
     
      public static void main(String[] args) {
     
        String str = "This is string";
        int i = 0;
        char[] chararr = str.toCharArray();
     
        System.out.print("Gimme a");
        for (char c : chararr) {
          System.out.print(" \"" + c + "\"! ");
          i++;
        }
     
        System.out.println("\nString length: " + i);
      }
    }

    There are a few more ways one could skin this String.
    Last edited by jdv; Yesterday at 02:05 PM. Reason: Missed an apostrophe

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,485
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Getting ArrayIndexOutBound Exception

    @nike: Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags which are explained near the top of the above link.

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