0

so I have to do a Flask Machine assignment and I have a small problem with the scanner class. I created a method in which I want to add the bottle types(A B or C) to an arraylist. So I don't really know how many bottles I will enter. The thing is I want my scanning to stop once I encounter a ,,0". I know I have to use a while loop but using it like this doesn't work because by the time I want to add the bottle to the list, it jumps to the next scanned bottle.

 While(input.next()!="0"){
  list.add(input.next());
  count++;

}

Cœur
  • 32,421
  • 21
  • 173
  • 232
CRD
  • 19
  • 4

2 Answers2

1

You should store the result of the input.next() callback in a variable and terminate the loop if it's equal to 0. For example:

while (true) {
   String next = input.next();
   if ("0".equals(next)) break;
   else {
      list.add(next);
      count++;
   }
}

More info about:

Community
  • 1
  • 1
Konstantin Yovkov
  • 59,030
  • 8
  • 92
  • 140
1

You call next() twice. Store the first value.

String next;
while (!(next = input.next()).equals("0"))
{
    list.add(next);
    count++;
}
RobAu
  • 17,042
  • 8
  • 69
  • 108