Character-by-character console input via Java 8 Stream

How can I convert console input System.in in a Stream of characters.

I tried this combination:

new BufferedReader(new InputStreamReader(System.in)).lines()

But the problem is that it outputs the entire string, and not a stream of characters, for example, I can't cut it using limit (). To clarify, I want to take a number from the console and turn it into an int array. I tried this method, but it also takes the numbers in one piece and in addition cuts off the zeros at the beginning if they were.

IntStream.of(new Scanner(System.in).nextInt()).limit(4).forEach(System.out::println)

I would like to pull from the input of the number character by character with the help of the stream and then somewhere in the intermediate stage turn it into an int and at the end add it to an array.

Thank you all for the answers, solved as follows:

int[] inArr = new BufferedReader(new InputStreamReader(System.in))
                .readLine()
                .chars()
                .filter(Character::isDigit)
                .map(Character::getNumericValue)
                .limit(3)
                .toArray();
Author: Artem, 2018-06-13

2 answers

You can do this:

String str = "hello world";
str
   .chars()
   .mapToObj(e -> (char) e)
   .limit(3)
   .forEach(System.out::println);

Conclusion:

H
e
l

 2
Author: Artem Konovalov, 2018-06-13 08:15:11

In the nine this question is solved elementary:

int[] ints = new Scanner(System.in)
               .tokens()
               .flatMapToInt(String::chars)
               .filter(Character::isDigit)
               .map(Character::getNumericValue)
               .limit(4)
               .toArray();

In the eight, you will have to twist a little:

public class Example {
    public static Stream<String> scannerStream(Scanner scanner) {
        Spliterator<String> spliterator
          = Spliterators.spliterator(scanner,
                                     Long.MAX_VALUE,
                                     Spliterator.ORDERED | Spliterator.NONNULL);
        return StreamSupport.stream(spliterator, false)
                            .onClose(scanner::close);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int[] ints = scannerStream(scanner)
                       .flatMapToInt(String::chars)
                       .filter(Character::isDigit)
                       .map(Character::getNumericValue)
                       .limit(4)
                       .toArray();

        System.out.println(Arrays.toString(ints));
    }
}
 4
Author: Sergey Gornostaev, 2018-06-13 13:28:19