简体   繁体   English

将扫描仪与定界符一起使用

[英]Using scanner with delimiter

I'm having some problems with scanning and storing data with an input that looks like this: 我在使用以下输入扫描和存储数据时遇到一些问题:

34 56=22 67=81 75 34 56 = 22 67 = 81 75
95 34 95 34
95 33 95 33
95 32 95 32

The "=" delimiter separates 3 completely different coordinates. “ =”分隔符分隔3个完全不同的坐标。 I have to put the last block of coordinates into an array but I can't do it to the first 2 coordinates. 我必须将坐标的最后一块放入数组中,但不能对前2个坐标进行处理。 Can someone help me please? 有人能帮助我吗?

I created a while loop that looks like the one below. 我创建了一个while循环,看起来像下面的循环。 What I want to create out of this input is start coordinates (the first two numbers), the end coordinates (the next to numbers between “=” signs), and an array with the rest of coordinates that are parted with “\\n”. 我要根据此输入创建的内容是开始坐标(前两个数字),结束坐标(在“ =”号之间的数字旁边)和一个数组,其余坐标与“ \\ n”分开。 My code only allows me to read the coordinates in the first line and store it in one coordinate start object. 我的代码仅允许我读取第一行中的坐标并将其存储在一个坐标起始对象中。 I want to part them into different objects/arrays. 我想将它们分成不同的对象/数组。

Scanner in = new Scanner(System.in);
    in.useDelimiter("=");
    while(in.hasNext()){
        Scanner coordinatesScanner = new Scanner(in.next());
        int coordinateX = coordinatesScanner.nextInt();
        int coordinateY = coordinatesScanner.nextInt();
        start = new Coordinates(coordinateX,coordinateY);
        out.printf("%d %d", start.x, start.y);
    }

You can change the delimiter with Scanner.useDelimiter and use regex to accept = aswell as whitespaces as delimiters: 您可以使用Scanner.useDelimiter更改定界符,并使用regex接受=以及空白作为定界符:

Scanner scanner = new Scanner(...);
scanner.useDelimiter("\\s|=");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM