简体   繁体   English

拆分成对数字的字符串,然后在 java 中拆分每个数字

[英]Splitting string of pairs of numbers, then splitting each one of them in java

lets say I have the following line:假设我有以下行:

ball_velocities:45,500 46,500 47,500

I would like to:我想:

  • split the pairs from each other将对彼此分开
  • split the the pair itself and the numbers inside of it from each other use both of those numbers in a function that I already have将这对本身和其中的数字彼此分开使用我已经拥有的 function 中的这两个数字
 String[] numbers = data.split("\\\\s+"); if (numbers.length > 0) { List<Velocity> velocities = new ArrayList<>(); for (String number: numbers) { try { int firstNum = Integer.parseInt(number); int secondNum = Integer.parseInt(number); velocities.add(Velocity.pair(firstNum,secondNum));

I know I have messed it up, so I'll be glad to hear some suggestions.我知道我搞砸了,所以我很高兴听到一些建议。

I think it's pretty simple, all I gotta do is to data.split by spaces as I already did data split again by comma and then I don't know how to combine those 2 numbers into one function.我认为这很简单,我要做的就是用空格分割数据,因为我已经用逗号再次分割了数据,然后我不知道如何将这两个数字组合成一个 function。

I mean in the end I want it to be: a list of velocities that contain the values of:我的意思是最后我希望它是:包含以下值的速度列表:

Velocity.pair(45,500)
Velocity.pair(46,500)
Velocity.pair(47,500)

Thanks.谢谢。

Assuming the class Velocity looks like this:假设 class Velocity如下所示:

class Velocity {
    private int firstNumber;
    private int secondNumber;

    public Velocity(int firstNumber, int secondNumber) {
        super();
        this.firstNumber = firstNumber;
        this.secondNumber = secondNumber;
    }

    public int getFirstNumber() {
        return firstNumber;
    }

    public void setFirstNumber(int firstNumber) {
        this.firstNumber = firstNumber;
    }

    public int getSecondNumber() {
        return secondNumber;
    }

    public void setSecondNumber(int secondNumber) {
        this.secondNumber = secondNumber;
    }

    public String toString() {
        return "[" + firstNumber + ", " + secondNumber + "]";
    }
}

you will basically have to go step by step:你基本上必须一步一步 go :

  1. remove the introducing tag ball_velocities: from the String you want to split,从要拆分的String中删除引入标签ball_velocities:
  2. split the result by an arbitrary amount of whitespaces, then用任意数量的空格split结果,然后
  3. split each result of that by comma,用逗号split每个结果,
  4. parse the results to int s将结果解析为int s
  5. instantiate a Velocity with the results of the parsing and finally用解析的结果实例化一个Velocity ,最后
  6. add each instance of Velocity to the List<Velocity>Velocity的每个实例添加到List<Velocity>

which can be done as follows, for example:可以按如下方式完成,例如:

public static void main(String[] args) throws ParseException {
    String data = "ball_velocities:45,500 46,500 47,500";

    List<Velocity> velocities = new ArrayList<>();
    // remove the intro tag and then split by whitespace(s)
    String[] numberPairs = data.replace("ball_velocities:", "").split("\\s+");

    // handle each result (which still consists of two numbers separated by a comma
    for (String numberPair : numberPairs) {
        // that means, split again, this time by comma
        String[] numbers = numberPair.split(",");
        // parse the results to ints
        int firstNum = Integer.parseInt(numbers[0]);
        int secondNum = Integer.parseInt(numbers[1]);
        // instantiate a new Velocity with the results and add it to the list
        velocities.add(new Velocity(firstNum, secondNum));
    }

    // print the list using the `toString()` method of Velocity
    velocities.forEach(System.out::println);
}

This example will print这个例子将打印

[45, 500]
[46, 500]
[47, 500]

Assuming that you have a string of data with velocity information, you can use the following snippet:假设您有一串包含速度信息的数据,您可以使用以下代码段:

@Getter
@Setter
@AllArgsConstructor
@ToString
public class Velocity {
    int id1;
    int id2;

    static Velocity pair(int i1, int i2) {
        return new Velocity(i1, i2);
    }
}
// -------- testing
String input = "ball_velocities:45,500 46,500 47,500";
String velo = input.replaceAll("ball_velocities\\s*\\:\\s*", ""); //remove prefix containing optional whitespaces

// String velo = "45,500 46,500 47,500";

Arrays.stream(velo.split("\\s+"))
      .map(s -> s.split("\\,"))
      .map(a -> Velocity.pair(Integer.parseInt(a[0]), Integer.parseInt(a[1])))
      .collect(Collectors.toList()) // list of velocity is available here
      .forEach(System.out::println);

output: output:

Velocity(id1=45, id2=500)
Velocity(id1=46, id2=500)
Velocity(id1=47, id2=500)

First let's get the inputs into a String array:首先让我们将输入放入一个String数组中:

String[] inputs = data.split(":")[1].split(" ");

Essentially we get the right-most part of the colon first and split that into an array of String items, which represent the input you have, having values like "45,500".本质上,我们首先获取冒号的最右侧部分,并将其splitString项数组,这些项表示您的输入,其值类似于“45,500”。 Let's create a Velocity array and fill it with items:让我们创建一个Velocity数组并用项目填充它:

Velocity[] velocities new Velocity[inputs.length];

for (int index = 0; index < inputs.length; index++) {
    String parts = inputs[index].split(",");
    velocities[index] = new Velocity(parts[0], parts[1]);
}

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

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