Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:35:51+00:00 2026-06-18T14:35:51+00:00

I get a StringIndexOutOfBoundsException in the terminating condition of my while loop . Could

  • 0

I get a StringIndexOutOfBoundsException in the terminating condition of my while loop. Could someone explain the reason for this?

import java.util.Scanner;
class Solution {

    public static int fun(String s) {
        int count=0;
        int k,j;                 

        for(int i=0;i<s.length();i++) {

            k=i;
            j=0;
            if (s.charAt(j) == s.charAt(k)) {
                while((s.charAt(j)==s.charAt(k))&&(k<s.length())&&(j<s.length())) {
                    j++;
                    k++;
                }
                count+=j;
            }
        }
        return count;
    }

    public static void main(String[] args) { 
        Scanner se=new Scanner(System.in);
        int t=se.nextInt();
        String s;
        int a[]=new int[t];
        for(int i=0;i<t;i++) {
            s=se.nextLine();
            a[i]=fun(s);
        }   
        for(int i=0;i<t;i++)
           System.out.println(a[i]); 
        se.close(); 
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:35:52+00:00Added an answer on June 18, 2026 at 2:35 pm

    From JavaDoc

    java.lang
    Class StringIndexOutOfBoundsException

    Thrown by String methods to indicate that an index is either negative
    or greater than the size of the string. For some methods such as the
    charAt method, this exception also is thrown when the index is equal
    to the size of the string.

    You are exceeding the length of the string.

    In addition I think you have some errors in your logic (see below).

    What you really are trying to do is this:

    while ((k < s.length()) && (j < s.length())) { // While no String goes out of Bounds
        if (s.charAt(j) != s.charAt(k)) { // If we get a different character
            break; // Get out of the loop
        } else {
            j++;  // Advance one position
            k++;
        }
    }
    

    What you were doing was this:

    if (s.charAt(j) == s.charAt(k)) { // If the characters are equal
        while ((s.charAt(j) == s.charAt(k)) // While the characters are equal
                  && (k < s.length()) && (j < s.length())) {  // And the position is smaller than the length
           j++;
           k++;
        }
        count += j;
    }
    

    The If is redundant, since you chech it in the while anyway, and count would be increased by zero.

    But more importantly, in the terminating condition of the while, you the check if s.charAt(j) happens before the check j < s.length(). Thus, you get the exception at the first case, before you see if j is to large


    In addition, since the expressions is Java are calculated from left to right, you could change your loop like this:

    while ((k < s.length()) && (j < s.length()) && (s.charAt(j) == s.charAt(k))) {
        j++;
        k++;
    }
    

    Now you don’t get an Exception, because if the first 2 terms are false (from the left), then the other two term on the right will not be evaluated at all (at least in my JVM)

    Output:

    run:
    2
    ababaa
    aa
    11
    3
    

    Hope that helped.

    Ps: I also changed the line

    int t = se.nextInt();
    

    to

    int t = se.nextInt();se.nextLine(); 
    

    So that you parse the newline after the number is given.


    Clarifications

    1) Why se.nextLine()

    You had

    int t = se.nextInt();
    

    Lets say the user enters 23 and presses enter, which means that the InputSream whill read from the keyboard 23\n. 23 is the number that the user entered, and \n is newline character. The newline character is used so that the computer can tell when one line ends and the next one begins, and it is automatically inserted when the user presses enter. More info here: How do I get a platform-dependent new line character?

    When you call nextInt(), you only read the number that was entered, but you do not read the \n character. Thus, next time you call readLine(), you will read the \n that is left over from when you entered the number (and pressed enter). This is the reason you change the above command to

    int t = se.nextInt();se.nextLine(); 
    

    Now you read that extra \n character, and the next call of nextLine(), that will happen when you read the string that the user entered, will correctly return the string.

    2) Why did whe change the loop to ((k < s.length()) && (j < s.length()) && (s.charAt(j) == s.charAt(k))

    You had this

    ( (s.charAt(j)==s.charAt(k)) && (k<s.length()) && (j<s.length()) )
    

    This caused the StringIndexOutOfBoundsException. And here is why:

    In Java, the expressions are calculated from left to right. that means, that at each iteration, the JVM will first check (s.charAt(j)==s.charAt(k)). If the term is true, then it will evaluate the term (k<s.length()), and if that is also true, it will evaluate (j<s.length()). If all these terms are true, the program will enter the loop.

    On the other hand, if the first term (i.e. (s.charAt(j)==s.charAt(k)) ) is false, then the whole expression is false (since we have and AND operator), and there is no need to calculate the rest of terms.

    Now, why did that cause an Exception? Look at what happens at the last iteration. At this point, the variable j (or k equivalently) will have a value equal to the length of string s. When the JVM tries to evaluate the termination condition, it will first evaluate the term (s.charAt(j)==s.charAt(k)). Since j is equal to the length of s, the call charAt() will throw a StringIndexOutOfBoundsException, since the call will try to get a character that is outside of the string. Remember that the indexes in the String are from 0 to length() - 1. This is where you got your Exception.

    If however, you change the termination condition to

    ((k < s.length()) && (j < s.length()) && (s.charAt(j) == s.charAt(k)))
    

    you will avoid the StringIndexOutOfBoundsException. Here is why. This time, the terms (k < s.length()) and (j < s.length()) are evaluated before the call to charAt(). Thus, when we reach the end of the string, at least one of the two first terms will be false, and there is no need to evaluate the rest of the Expression. Thus, at the last iteration, the method charAt is not called at all, so we don’t get the Exception.

    I hope this clarified the situation a bit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When minimising yui with 2.4.6, I get this problem: java.lang.StringIndexOutOfBoundsException: String index out of
//get the lat and loni throgh the loaddata function.java.util.ConcurrentModificationException at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:64) How to resolve
I get this error while accessing a php script: W/System.err: Error reading from ./org/apache/harmony/awt/www/content/text/html.class
I get the following error in my java code: Exception in thread main java.lang.StringIndexOutOfBoundsException:
get topLeft() { return this._topLeft; } set topLeft(value) { this._topLeft = value; Recalc(); }
Why does the following Java code snippet throw a StringIndexOutOfBoundsException on the third line
In Java, when I do: a/b/c/d.replaceAll(/, @); I get back a@b@c@d But when I
EVerytime I write any code similar to this one, I get this type of
Get a problem when installing java application as service in windows environment(Win 7). use
get this from my database: 252.587254564 Well i wanna remove the .587254564 and keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.