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