String index out of bounds? (Java, substring)

This program I am doing for the COSC course does not compile correctly, I keep getting the error:

Exception on stream "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2

at java.lang.String.substring (String.java:1765) at VowelCount.main (VowelCount.java:13)

Here's my code:

import java.util.Scanner;

public class VowelCount {
 public static void main(String[] args) {
  int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0;
  String input, letter;
  Scanner scan = new Scanner (System.in);

  System.out.println ("Please enter a string: ");
  input = scan.nextLine();

  while (count <= input.length() ) {
   letter = input.substring(count, (count + 1));

   if (letter == "a") {
    a++; }
   if (letter == "e") {
    e++; }
   if (letter == "i") {
    i++; }
   if (letter == "o") {
    o++; }
   if (letter == "u") {
    u++; }

   count++;

  }
  System.out.println ("There are " + a + " a's.");
  System.out.println ("There are " + e + " e's.");
  System.out.println ("There are " + i + " i's.");
  System.out.println ("There are " + o + " o's.");
  System.out.println ("There are " + u + " u's.");
 }
}

      

As far as I know, this should work, but why? Any help would be great. Thank!

+2


source to share


5 answers


You may need to take out = in line

while (count <= input.length() ) {

      

and do it

while (count < input.length() ) {

      

because it causes a substring read beyond the length of the string.

=============== But I'll add some extra tips even though it wasn't asked for:



don't use == to compare strings, use

letter.equals("a")

      

instead of this. Or even better, try using

char c = input.charAt(count);

      

to get the current character, then compare like this:

c == 'a'

      

+5


source


Removing the equal sign should fix this.

while (count < input.length()) {

and since you want to get one character, you have to do this:



substr(count,1)

because the second parameter is actually the length, not the index.

0


source


I think your loop condition should be count < input.length

. Right now, the last iteration is working from count == length

, so your tag is substring

assigned a starting index after the last character in the string, which is illegal. These types of bounding errors are very common when writing such loops, so it is always helpful to double and triple check the loop conditions when you encounter such an error.

Also, comparing strings with an operator will ==

usually not do what you want. This compares if two variables refer to the same object. Instead, you want to test string1.equals(string2)

that compares the contents of two strings.

0


source


Fixed this with the help of everyone, and especially Vincent. Thank! Runs wonderfully.

import java.util.Scanner;

public class VowelCount {
    public static void main(String[] args) {
        int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0;
        String input;
        char letter;

        Scanner scan = new Scanner (System.in);

        System.out.print ("Please enter a string: ");
        input = scan.nextLine();

        while (count < input.length() ) {
            letter = input.charAt (count);

            if (letter == 'a')
                a++; 
            if (letter == 'e') 
                e++; 
            if (letter == 'i') 
                i++; 
            if (letter == 'o') 
                o++; 
            if (letter == 'u') 
                u++; 

            count++;

        }
        System.out.println ("There are " + a + " a's.");
        System.out.println ("There are " + e + " e's.");
        System.out.println ("There are " + i + " i's.");
        System.out.println ("There are " + o + " o's.");
        System.out.println ("There are " + u + " u's.");
    }
}

      

0


source


Before loop, try below

if(input.length()>0){
//you code
}

      

0


source







All Articles