M
M
mr_blond972017-07-02 15:02:56
Java
mr_blond97, 2017-07-02 15:02:56

How to fill an array with numbers from a string?

The input is a string with 4 numbers. How to fill an array with numbers from a string?

//строка "11 0 0 17"
byte[] byteArray = new byte[4];
         		
byteArray[0] = //первое      из "11 0 0 17"
byteArray[1] = //второе      из "11 0 0 17"
byteArray[2] = //третье       из "11 0 0 17"
byteArray[3] = //четвертое из "11 0 0 17"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-07-02
@mr_blond97

String str = "11 0 0 17";
byte[] byteArray = new byte[4];

String[] strArray = str.split(" ");
for (int x = 0; x < strArray.length; x++) {
    if (x >= byteArray.length)
        break;

    byteArray[x] = Byte.parseByte(strArray[x]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question