[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now



Posted on 2006-05-19
Medium Priority
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
  • 2
LVL 23

Expert Comment

ID: 16723960
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

basicinstinct earned 520 total points
ID: 16723973
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
LinkedIn blogging is great for networking, building up an audience, and expanding your influence as well. However, if you want to achieve these results, you need to work really hard to make your post worth liking and sharing. Here are 4 tips that ca…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

872 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