Hi, Everyone, recently my programming class has started making programs involving arraylists. Our program is supposed to delete all numbers in the arraylist that contain numbers that end in the number we input and also numbers that are either +1 or -1 from the number.
Ex.
arrayList = [43, 59, 87, 60, 67, 25]
What number do you wish to input?
8
arrayList = [43, 60, 25]
^ This is the output that should occur. Removes all numbers that end in 8, +1(9) and -1(7) of it.
However, this is the real result I get. Ex.
arrayList = [43, 59, 87, 60, 67, 25]
What number do you wish to input?
8
arrayList = [43, 87, 60, 25]
The 87 remains even though it should be gone. Does anyone know what the problem is? Here is the for loop I used to check from numbers that were equal to the number, +1, and -1 of that number.
for (int ind = 0; ind<nbrs.size() ; ind++) {
if (nbr > 0 || nbr < 9) {
if ((nbrs.get(ind) %10) == (nbr) || (nbrs.get(ind) %10) == (nbr+1) || (nbrs.get(ind) %10) == (nbr-1)) {
nbrs.remove(ind );
}
}
else if (nbr == 9) {
if ((nbrs.get(ind) %10) == nbr || (nbrs.get(ind) %10) == (nbr-1)) {
nbrs.remove(ind );
}
}
else if (nbr == 0) {
if ((nbrs.get(ind) %10) == nbr || (nbrs.get(ind) %10) == (nbr+1)) {
nbrs.remove(ind );
}
}
}
Ex.
arrayList = [43, 59, 87, 60, 67, 25]
What number do you wish to input?
8
arrayList = [43, 60, 25]
^ This is the output that should occur. Removes all numbers that end in 8, +1(9) and -1(7) of it.
However, this is the real result I get. Ex.
arrayList = [43, 59, 87, 60, 67, 25]
What number do you wish to input?
8
arrayList = [43, 87, 60, 25]
The 87 remains even though it should be gone. Does anyone know what the problem is? Here is the for loop I used to check from numbers that were equal to the number, +1, and -1 of that number.
for (int ind = 0; ind<nbrs.size() ; ind++) {
if (nbr > 0 || nbr < 9) {
if ((nbrs.get(ind) %10) == (nbr) || (nbrs.get(ind) %10) == (nbr+1) || (nbrs.get(ind) %10) == (nbr-1)) {
nbrs.remove(ind );
}
}
else if (nbr == 9) {
if ((nbrs.get(ind) %10) == nbr || (nbrs.get(ind) %10) == (nbr-1)) {
nbrs.remove(ind );
}
}
else if (nbr == 0) {
if ((nbrs.get(ind) %10) == nbr || (nbrs.get(ind) %10) == (nbr+1)) {
nbrs.remove(ind );
}
}
}
Comment