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

Thread: IndexOutOfBoundsException error (array of strings)

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

    Default IndexOutOfBoundsException error (array of strings)

    Hello guys, I'm a beginner and I'm trying to learn Java all by myself so please, go easy on me. Now, I was trying to make this apparently easy program work, it should return all the strings (from an array filled by a user) which start with an uppercase letter. As mentioned above, every time I run it, that error appears in my screen.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at javaapplication5.JavaApplication5.main(JavaApplica tion5.java:19)
    Here is the code. Thanks in advance for all the help and please forgive me if it's something relatively stupid!

    package javaapplication5;
    import java.util.Scanner;
    import java.lang.String;
     
     
    public class JavaApplication5 {
     
     
        public static void main(String[] args) {
     
            System.out.println("How many words does your array have?");
            Scanner s = new Scanner(System.in);
            int l = s.nextInt();
            String[] array = new String[l];
            System.out.println("Type the words.");
            for (int i=0; i<l+1; i++)
            {
                array[i] = s.nextLine();
            }
            System.out.println("The words starting with an uppercase letter are:");
            for (String st : array)
            {
                char c = st.charAt(0);
                if (c>='A' && c<='Z') System.out.print(st);
     
            }
     
        }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,080
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: IndexOutOfBoundsException error (array of strings)

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at javaapplication5.JavaApplication5.main(JavaApplica tion5.java:19)
    At line 19 the program used an index into an array that was past the end of the array. Remember that the maximum array index is the array's length minus 1. An index of 3 requires that the array have 4 or more elements.
    Check the code at line 19 to see why it tried to use an index past the end of the array.

    Note: single letter variable names can make the code harder to read, especially the letter l which can be confused with I or 1.
    Use names that describe what the variable contains. For example: numberOfWords
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: IndexOutOfBoundsException error (array of strings)

    Alright, thanks for the suggestion, but I still couldn't get it. First off, I tried to make it more intuitive:
    package javaapplication5;
    import java.util.Scanner;
    import java.lang.String;
     
     
    public class JavaApplication5 {
     
     
        public static void main(String[] args) {
     
            System.out.println("How many words does the array have?");
            Scanner scan = new Scanner(System.in);
            int leng = scan.nextInt();
            String[] arrayOfStrings = new String[leng];
            System.out.println("Type the words.");
            for (int i=0; i<arrayOfStrings.length; i++)
            {
                arrayOfStrings[i] = scan.nextLine();
            }
            System.out.println("The words that start with an uppercase letter are:");
            for (String st : arrayOfStrings)
            {
                char c = st.charAt(0);
                if (c>='A' && c<='Z') System.out.print(st);
     
            }
     
        }
     
    }

    So, line 19 is arrayOfStrings[i] = scan.nextLine();.
    Basically, what I wanted, was for each string in the array to be filled. If I'm taking your suggestion correctly, I guess I should change the limit condition in the for, and remove the +1? In this case, that's what I get.

    run:
    How many words does the array have?
    6
    Type the words.
    I
    Swear
    I'm
    Trying
    Goddamnit
    The words that start with an uppercase letter are:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:658)
    at javaapplication5.JavaApplication5.main(JavaApplica tion5.java:23)
    I know it's supposed to be something simple and I thought I knew how array indices are supposed to work, but I swear I have no idea on how to fix this.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,080
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: IndexOutOfBoundsException error (array of strings)

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:658)
    at javaapplication5.JavaApplication5.main(JavaApplica tion5.java:23)
    The code at line 23 used an index past the end of the String. The index of 0 requires that the String have at least 1 character. If the String is empty it does not have a character at the first location: index of 0.
    Check that the String has the needed number of characters before trying to access one.

    Your using the array's .length field is the standard way to iterate through an array's contents.

    To see what is in an array use the Arrays class's toString method:
      System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    Note: The console printout only shows that 5 words were entered. Did you hit Enter an extra time?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Return an array of Pairs that have the same length as the input array of Strings.
    By hemla in forum Object Oriented Programming
    Replies: 3
    Last Post: August 8th, 2013, 08:17 AM
  2. 2D Array with Strings
    By xionyus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2012, 06:35 AM
  3. How do I combine two strings in an array?
    By SkyAphid in forum Collections and Generics
    Replies: 3
    Last Post: September 15th, 2011, 06:20 PM
  4. Array of strings Null Pointer error
    By FredrichGauss in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 12:43 AM
  5. creating an array of strings?
    By Jason in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2010, 08:28 AM