-4

I'm getting StringIndexOutOfBoundsException:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 3, end 4, length 3 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3116) at java.base/java.lang.String.substring(String.java:1885) at ReadZILLA.main(Main.java:26)

from the following code:

import java.io.*;
import java.text.*;

class ReadZILLA {

public static void main(String[] args) throws IOException {
    int vowel = 0;
    int words = 0;
    int line = 10;
    int line_no = 11-line;
    int n = 1;
    String a = "a";
    String e = "e";
    String i = "i";
    String o = "o";
    String u = "u";
    String A = "A";
    String E = "E";
    String I = "I";
    String O = "O";
    String U = "U";
    String space = " ";
    BufferedReader in_text = new BufferedReader(new InputStreamReader(System.in));
    String text = in_text.readLine();

    while(line > 1){
        String char_def = text.substring(n-1,n);
        System.out.println(char_def);

        if (char_def == a || char_def == e || char_def == i || char_def == o || char_def == u || char_def == A || char_def == E || char_def == I|| char_def == O || char_def == U){
            vowel = vowel + 1;
        } else if (char_def == space){
            words = words +1;
        } else if (char_def == "\n"){
            System.out.println ("Numbers of vowels: " + vowel + "in line " + line_no);
            vowel = 0;
            line = line-1;

        }

        n = n +1;

    }

    int words_avg = words/10;
    System.out.println("Number of words: " + words);
    System.out.println("Average number of words: " + words_avg);

}

The program is called ReadZILLA and it's supposed to output the numbers of vowels, words and average words in a line from the input. E.G input: tHe DoG is BrOWN /n thE cat IS rED output: vowels: 8, words: 8, average words per line: 4

user207421
  • 305,947
  • 44
  • 307
  • 483
Hieu Nguyen
  • 5
  • 1
  • 1
  • 1
  • *"How can I fix the following error"* Don't call `substring(3, 4)` on a string that is only 3 long. *Hint:* [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Dec 06 '17 at 22:20
  • 1
    looks like you are trying to get a character; use charAt() – Doc Dec 06 '17 at 22:25
  • You keep incrementing `n` in a `while` loop and then use `n` to `text.substring(n-1,n)` However, `n` gets higher than `text.length()` and therefore you get `StringIndexOutOfBoundsException` – iaforek Dec 06 '17 at 22:37
  • *See also:* [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) --- None of your `if` statements will ever be true. – Andreas Dec 06 '17 at 22:39
  • And realize that the string returned by `readLine()` will never contain `\n`, so `line` will never be decremented. – Andreas Dec 06 '17 at 22:42
  • This doesn't have anything whatsoever to do with `BufferedReader` or [tag:indexoutofboundsexception]. Don't tag indiscrimately, and keep your title relevant. – user207421 Dec 07 '17 at 01:04

1 Answers1

2

Let's try to understand the problem. In order to do that, first you need to understand what is StringIndexOutOfBoundsException.

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."

So, you are trying to reach some index that is higher than size of the chars in it or negative.

Also if you want to reach character n you should give index=n-1.

Your exception says that you are trying to access characters between 3 and 4 but length is 3.

Here:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 3, end 4, length 3

So, you have to add if statement that checks if increased n once more or continue with next line.

Furkan Yavuz
  • 1,858
  • 5
  • 30
  • 51