0

I've read through a number of similar threads, but in my nOObishness can't seem to figure out how I can apply the answers to my code. Sorry...

When running the code below I'm getting the error: java.lang.StringIndexOutOfBoundsException: String index out of range:

The outcome of the program is okay. I'm getting all the capitals from the string that I have entered with the scanner. But the error message is kinda in the way.

Any help is very appreciated! Thanks in advance!

import java.util.Scanner;

public class TestClass {

    public static void main(String[] args) {

        Scanner in3 = new Scanner(System.in);
        System.out.print("Enter a senctence with capitals: ");
        String inputString = in3.nextLine();

        int stringLength = String.valueOf(inputString).length();
        int i = 0;

        while (i <= stringLength)
        {
            int subsStart = i;
            int subsEind = i + 1;

            String stringToCheck = inputString.substring(subsStart, subsEind);

            char letterToCheck = stringToCheck.charAt(0);

            if (Character.isUpperCase(letterToCheck)) 
            {
                System.out.println(letterToCheck);
            }
            i++;

        }
        in3.close();

    }

}
5
  • What happens when i == stringLength? How does substring work? Commented Feb 12, 2017 at 20:38
  • And what happens if i == stringLength - 1? Commented Feb 12, 2017 at 20:39
  • And why do you need to String.valueOf something that's already a string? Commented Feb 12, 2017 at 20:40
  • You should also have a look at for loop. Its more suitable than while in your case. Commented Feb 12, 2017 at 20:41
  • Thanks for all the feedback guys. Just getting started in Java and I appreciate you're comments and will check them all! Commented Feb 12, 2017 at 20:42

3 Answers 3

0

You have a couple of places where you are indexing off the end of the string. First, your main loop should not go through stringLength because the length of the string is an illegal index into the string. At most, the loop should go through stringLength - 1, which means the loop should be written

while (i < stringLength) // NOT i <= stringLength

But there's another problem. Inside the loop, you are computing

String stringToCheck = inputString.substring(subsStart, subsEind);

and then later evaluate

char letterToCheck = stringToCheck.charAt(0);

which makes the assumption that stringToCheck is not the empty string. Unfortunately, that assumption fails when subsStart == stringLength. Therefore you need to stop your outer loop before stringLength:

while (i < stringLength - 1)

However, since you're only interested in one letter at a time, I wouldn't bother with extracting the substring. Also, you have a lot of redundant variables in your code. I would write it something like this:

public static void main(String[] args) {

    Scanner in3 = new Scanner(System.in);
    System.out.print("Enter a sentence with capitals: ");
    String inputString = in3.nextLine();

    int stringLength = inputString.length();

    for (int i = 0; i < stringLength; i++)
    {
        char letterToCheck = inputString.charAt(i);

        if (Character.isUpperCase(letterToCheck)) 
        {
            System.out.println(letterToCheck);
        }

    }
    // in3.close(); // not really needed for System.in
}
Sign up to request clarification or add additional context in comments.

Comments

0
while (i <= stringLength)

should be

while (i < stringLength)

For eaxample, hello has string length of 5, but in terms of indexing, it is only up to 4.. 0 1 2 3 4

1 Comment

Actually, while (i < stringLength - 1)
0

Last two iterations will fail.

String stringToCheck = inputString.substring(subsStart, subsEind);

Tries to read indexes stringLength & stringLength+1 but there are only stringLength-1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.