I read a file and add it to a list then read the list and split the strings and compare it and do somthing with it.
i get this exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
Where should i correct my code?
for (Productname m : listIP) {
if (m.getIdentifier() == null || m.getProductname() == null) {
addToNonSimilarList( m.getProductname());
} else {
String id = m.getIdentifier().replaceAll("(?<=[A-Za-z])(?=[0-9])|(?<=[0-9])(?=[A-Za-z])", " ").toUpperCase();
String product = m.getProductname().replaceAll("(?<=[A-Za-z])(?=[0-9])|(?<=[0-9])(?=[A-Za-z])", " ").toUpperCase();
id = id.replaceAll("\\s+", " ");
product = product.replaceAll("\\s+", " ");
if (!id.equalsIgnoreCase(product)) {
if (id.contains(" X ") && product.contains(" X ")) {
String[] ide = id.split(" (?=X\\s*\\d+)");
String[] prod = product.split(" (?=X\\s*\\d+)");
System.out.println(m.getMnemonic());
if (ide.length > 0 && prod.length > 0 && ide[1].trim().equalsIgnoreCase(prod[1].trim())) {
String[] i = ide[0].split(" (?=\\d+)");
String[] p = prod[0].split(" (?=\\d+)");
if (i[0].trim().equalsIgnoreCase(p[0].trim())) {
//do nothing
} else {
addToNonSimilarList( m.getProductname());
}
} else {
addToNonSimilarList( m.getProductname());
}
} else {
addToNonSimilarList( m.getProductname());
}
}
}
}
Well, this condition looks dodgy:
You’re testing if there’s at least one element in each, but you’re using the second element in each.
It’s hard to tell what you’re trying to achieve, but I suspect you either want to change the
lengthconditions or the array indexes. I’d also split this method into shorter ones, and probably create well-named static variables with references toPatternobjects, for clarity…EDIT: If you genuinely want to use the second element from each array, I’d suggest: