String index out of bounds? (Java, substring loop)

前端 未结 5 578
感情败类
感情败类 2021-01-20 17:26

This program I\'m making for a COSC course isn\'t compiling right, I keep getting the error:

Exception in thread \"main\" java.lang.StringIndexOutOfBoundsException:

相关标签:
5条回答
  • 2021-01-20 18:06

    You may need to take out the = in the line

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

    and make it

    while (count < input.length() ) {
    

    because it is causing the substring to read beyond the length of the string.

    =============== But I'll add a few extra bits of advice even though its not asked for:

    do not use == to compare strings, use

    letter.equals("a")
    

    instead. Or even better, try using

    char c = input.charAt(count);
    

    to get the current character then compare like this:

    c == 'a'
    
    0 讨论(0)
  • 2021-01-20 18:07

    Before loop,try below

    if(input.length()>0){
    //you code
    }
    
    0 讨论(0)
  • 2021-01-20 18:14

    Fixed it with help from everyone, and especially Vincent. Thank you! 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 讨论(0)
  • 2021-01-20 18:18

    I think your loop condition should be count < input.length. Right now, the last iteration runs with count == length, so your substring call is given a start index after the last character in the string, which is illegal. These type of boundary errors are very common when writing such loops, so it's always good to double- and triple-check your loop conditions when you encounter a bug like this.

    Also, comparing strings with the == operator usually won't do what you want. That compares whether or not the two variables reference the same object. Instead, you want to test string1.equals(string2), which compares the contents of the two strings.

    0 讨论(0)
  • 2021-01-20 18:29

    Removing the equal sign should fix that.

    while (count < input.length()) {

    and since you want to get a single character, you should do this:

    substr(count,1)

    because the 2nd parameter is actually length, not index.

    0 讨论(0)
提交回复
热议问题