I'm learning Java and I don't know what is wrong here. Why is this error happening? I don't see anything wrong and that was actually working until when I wrote the line "count = 0" before the second "for" loop.
That is the error: java.lang.StringIndexOutOfBoundsException: String index out of range: 1
That's the line where the error is happening:
if(mots.get(j).startsWith(searchPhrase.substring(0,1))){
Here is the entire code:
import java.util.*;
public class Test {
public static void main(String[] args) {
List<String> mots = new ArrayList<>();
List<String> saida = new ArrayList<>();
mots.add("un");
mots.add("deux");
mots.add("trois");
String searchPhrase = "aaasdeuxctsundesle";
int count = 0;
int countAnula = 0;
int azul = 0;
String anula = "-"; //Tem que fazer cast para char depois
String frase = searchPhrase;
for (int i = 0; i < frase.length(); i++) {
count = 0;
for (int j = 0; j < mots.size(); j++) {
if (mots.get(j).startsWith(searchPhrase.substring(0,1))) {
for (int t = 0; t < mots.get(j).length(); t++) {
if (searchPhrase.charAt(t) == mots.get(j).charAt(t)) {
azul ++;
} else {
break;
}
}
if (azul == mots.get(j).length()) {
saida.add(mots.get(j));
searchPhrase = searchPhrase.substring(mots.get(j).length());
j = 0;
azul = 0;
} else {
searchPhrase = searchPhrase.substring(1);
saida.add(anula);
j = 0;
azul = 0;
}
} else {
count ++;
}
}
if (count == mots.size()) {
searchPhrase = searchPhrase.substring(1);
saida.add(anula);
count = 0;
}
}
for (int g = 0; g < saida.size(); g++) {
System.out.println(saida.get(g));
}
}
}
First of all, congrats on getting started on Java. It's an amazing language and the possibilities are endless. Here is how you can go about fixing your error.
The first step is to understand exactly what your error means, so let's get to that!
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
The type of exception thrown is a StringIndexOutOfBoundsException. Anytime you get an IndexOutOfBoundsException (or any type thereof) it means that you are trying to access an index in an array that doesn't exist. By calling the substring method, you are dividing the string in a character array, and selecting characters A to B (In your case 0 to 1). If character 1 doesn't exist, and yet you try to access it, it will throw this error.
The reason you are getting this error is therefore that you are trying to execute a substring(0,1) on a String with less than 1 character, aka an empty string or even a null string maybe.
Also, to give you a quick tip. Typically, a you should create a new method if your method body extends 12 lines. This will hugely increase the readability of your code. It took me a while to find the right place for your fix :-)
Change
if (mots.get(j).startsWith(searchPhrase.substring(0, 1))) {
to
if (searchPhrase != null && searchPhrase.length() > 0 && mots.get(j).startsWith(searchPhrase.substring(0, 1))) {
By doing this you check if the length of your string is sufficient before executing the substring method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With