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();

    }

}
Bertje
  • 13
  • 1
  • 4
  • What happens when i == stringLength? How does substring work? – OneCricketeer Feb 12 '17 at 20:38
  • And what happens if `i == stringLength - 1`? – Guy Feb 12 '17 at 20:39
  • And why do you need to `String.valueOf` something that's already a string? – OneCricketeer Feb 12 '17 at 20:40
  • You should also have a look at [for loop](https://www.google.co.il/search?q=for+loop+java&oq=for+loop+java&aqs=chrome..69i57j0l5.2624j0j4&sourceid=chrome&ie=UTF-8). Its more suitable than `while` in your case. – Guy Feb 12 '17 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! – Bertje Feb 12 '17 at 20:42

3 Answers3

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

Luminous_Dev
  • 614
  • 6
  • 14
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
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
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.

hartwecm
  • 219
  • 1
  • 4