I am trying to find words in an array that end with a letter 'a'. I thought of doing it using two for loops but I keep getting integer out of bounds error.
Could anyone tell me what i am doing wrong?
The code:
Scanner sc = new Scanner(System.in);
System.out.println("Enter text: ");
String text = sc.nextLine();
String[] words = text.split(" ");
for (int i = 0; i < words.length; i++) {
words[i] = words[i] + " ";
}
for (int i = 0; i < words.length ; i++) {
for (int j = 0; j <= words[i].charAt(j); j++) {
if (words[i].charAt(j) == 'a' && words[i].charAt(j + 1) == ' ') {
System.out.println(words[i]);
}
}
}