Is there an equivalent method to C's scanf in Java?

前端 未结 7 1173
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 11:58

Java has the notion of format strings, bearing a strong resemblance to format strings in other languages. It is used in JDK methods like String#format() for output conversio

7条回答
  •  独厮守ぢ
    2020-12-01 12:26

    Not an equivalent, but you can use a Scanner and a pattern to parse lines with three non-negative numbers separated by spaces, for example:

    71 5796 2489
    88 1136 5298
    42 420 842
    

    Here's the code using findAll:

    new Scanner(System.in).findAll("(\\d+) (\\d+) (\\d+)")
            .forEach(result -> {
                int fst = Integer.parseInt(result.group(1));
                int snd = Integer.parseInt(result.group(2));
                int third = Integer.parseInt(result.group(3));
                int sum = fst + snd + third;
                System.out.printf("%d + %d + %d = %d", fst, snd, third, sum);
            });
    

提交回复
热议问题