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
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 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
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 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; July 29th, 2014 at 02:05 PM. Reason: Missed an apostrophe

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 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, 10:01 AM
  3. Replies: 1
    Last Post: February 6th, 2013, 11:21 AM
  4. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  5. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM