Posted on 2006-05-19
Last Modified: 2010-04-01
Hi, i out of sudden have this error :

java.lang.StringIndexOutOfBoundsException: String index out of range: 50

and it's pointing to this code:

                        for (int i = 0; i < keyword_entered.length; i++) {
                              String temp_keyword = null;

                              if (keyword_entered.length > 50) {
                              >>>>>      temp_keyword = keyword_entered[i].substring(0, 50);
                              } else {
                                    temp_keyword = keyword_entered[i];

The data in the keyword_entered is something like this:
320789 320790 320791 320792 320793 320780 320781 320782 320783 320784 320785 320786 320787 320788 320035 320036 320037 320038 320752 320753 320754 320755 320756 320757 320758 320759 320760 320761 320762 320776 320777 320778 320779 320016 320017 320018 320019 320020 320021 320022 320023 320024 320025 320026 320027

and the main objective of the code that i am writting is only to elimate the extra 50 more character carry by the array above, if any.

So, how can i tackle this problem?
Question by:wjh7554
    LVL 23

    Expert Comment

    Well there is an error in the code...

    keyword_entered is an array, so "keyword_entered.length" refers to how many elements there are in the array

    if (keyword_entered.length > 50)  //Here you find out if the array has more than 50 elements
    keyword_entered[i].substring(0, 50);  //Here you are accessing an individual element of the array and asking for the substring from character '0 to 50'.  

    The StringIndexOutOfBoundsException is being thrown because the string in keyword_entered[i] is not 50 characters long.

    What are you trying to do exactly?

    LVL 23

    Accepted Solution

    If you are trying to make sure that no individual element in the array is longer than 50 characters then change your code to this:

    for (int i = 0; i < keyword_entered.length; i++) {
        String temp_keyword = null;
        if (keyword_entered[i].length() > 50) //the change is here
               temp_keyword = keyword_entered[i].substring(0, 50);
               temp_keyword = keyword_entered[i];

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    OfficeMate Freezes on login or does not load after login credentials are input.
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now