Skip to content

Instantly share code, notes, and snippets.

@rabestro
Created March 24, 2021 17:11
Show Gist options
  • Save rabestro/babba5fcd6bb49e9acbe3e60ae64c51d to your computer and use it in GitHub Desktop.
Save rabestro/babba5fcd6bb49e9acbe3e60ae64c51d to your computer and use it in GitHub Desktop.
The length of the sequence

For its input, the program gets a sequence of non-negative integers; each integer is written in a separate line. The sequence ends with an integer 0; when the program reads 0, it should end its work and output the length of the sequence (not counting the final 0).

Don’t read numbers following the number 0.

import java.util.Scanner;
import static java.util.function.Predicate.not;
class Main {
public static void main(String[] args) {
System.out.println(
new Scanner(System.in)
.tokens()
.takeWhile(not("0"::equals))
.count()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment