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

Thread: java.lang.ArrayIndexOutOfBoundsException ???

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post java.lang.ArrayIndexOutOfBoundsException ???

    hello, i'm rather new to java and this is an assignment for my programming class. i keep getting the error in the title- i did look up what it means, but honestly i'm still clueless on what to do. can someone please tell me what i'm doing wrong?
    here's my code:

     import java.io.*;
    import java.util.*;
    public class occurance { 
        public static void main(String args[]) {
            Scanner TMKF = new Scanner(System.in);
            System.out.println ("Please input a sentence and a letter that you want a count of.");
            String sentance = TMKF.nextLine();
            String letter = TMKF.nextLine();
            int letterCount = 0;
            int n=-1;
            String[] arr = new String[sentance.length()];
            for( int i = 0; i < sentance.length(); i++){
                arr[i] = String.valueOf(sentance.charAt(i) );
            }
            do {
                n++;
                if(arr[n]==letter) {
                    letterCount++;
                }
            } while (n < arr.length); 
            System.out.println (sentance + " has " + letterCount + " occurance(s) of the letter " + letter);
        }
    }

    java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
    at occurance.main(occurance.java:17)

    ^the full error message

    thank you in advance.
    Last edited by tordue; December 19th, 2020 at 01:58 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException ???

    Please copy the full text of the error message and paste it here showing what line the exception is on and what the bad value was.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException ???

    The problem begins at your do while loop.
    Say arr.length() == 3 (so that means the loop should run 3 times, 0 < 3, 1 < 3, 2 < 3)
    The problem is when it reaches the last time - it checks if 2 (n) is less than 3 (arr.length) and it executes the do block of code again -
    this time when it starts the last time of the loop it will increase n to 3 - which causes the problem:
    There is no arr[3] - because if the length of the array is 3 - the highest one will be arr[2] (0, 1, 2).

    I'd recommend you set n to 0 at the beginning and do a simplistic while loop without the do section to not confuse yourself.
    Hope you get it.

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException error
    By incxx in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 30th, 2013, 04:45 PM
  2. java.lang.ArrayIndexOutOfBoundsException help
    By madjavapuppy in forum What's Wrong With My Code?
    Replies: 25
    Last Post: August 6th, 2012, 05:31 PM
  3. java.lang.ArrayIndexOutOfBoundsException
    By rajesh3006 in forum Member Introductions
    Replies: 1
    Last Post: March 12th, 2012, 07:32 AM
  4. java.lang.ArrayIndexOutOfBoundsException - huh?
    By mrUnknown in forum Exceptions
    Replies: 3
    Last Post: February 18th, 2012, 09:06 AM
  5. [SOLVED] java.lang.ArrayIndexOutOfBoundsException
    By anmaston in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2011, 04:43 PM