I'm looking for a way to use scanner in a while loop, without advancing twice.
String desc = "";
while (!scanner.next().equals("END")) {
desc = desc + scanner.next();
}
As you can see, when scanner.next()
is called in the while loop's condition and inside of the while loop itself. I want it to advance the scanner only once. Is there any way to do that?
Yes it is possible. You should also check that the Scanner
has more tokens in its input
String desc = "";
String next = null;
while (scanner.hasNext() && !(next = scanner.next()).equals("END")) {
desc = desc + next;
}
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