Parse a negative prefix integer from string in java

后端 未结 3 1563
天命终不由人
天命终不由人 2021-01-25 05:50

Hi i have a string looking something like this 10 -1 30 -2 and i want to read the numbers between spaces. I can do this using a FOR statement and the code

Chara         


        
3条回答
  •  被撕碎了的回忆
    2021-01-25 06:34

    Is this what you want?

    for (String number : "10 -1 30 -2".split("\\s"))
    {
        int x = Integer.parseInt(number);
        System.out.println(x);
    }
    

    This will print:

    10
    -1
    30
    -2
    

提交回复
热议问题