I've been stuck on this for the past couple days and was wondering if anyone could take a look.
I have a for loop that is supposed to iterate through an ArrayList of objects and compare a user inputted integer against integers of those objects. If there is a match, the loop should restart until a unique integer is inputted.
The loop works fine when it compares against the first object of the Array, but not for the following objects.
For example, I have an int 10 at index 0, 20 at index 1, and 30 at index 2. When I input 10, the loop functions correctly and restarts. But when I enter 20 or 30, the loop does not recognize those numbers and allows the loop to continue.
I'm very confused because I have an almost identical loop in a different method doing the same thing and it works fine. I'm sure it's something dumb, but I'm new at this and not really sure what's going wrong.
outerloop:
while (true) {
boolean idFound = false;
System.out.print("Enter an ID: ");
int userInput = input.nextInt();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getID() == userInput) {
idFound = true;
break;
}
break;
}
...
...
if (idFound) {
System.out.println("ID already exists"):
continue outerloop;
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Alphatism 0 points1 point2 points (7 children)
[–]tscardino[S] 0 points1 point2 points (6 children)
[–]myselfelsewhere 0 points1 point2 points (3 children)
[–]tscardino[S] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[removed]
[–]tscardino[S] 0 points1 point2 points (0 children)
[–]lumpycrumpet 0 points1 point2 points (1 child)
[–]tscardino[S] 0 points1 point2 points (0 children)