문제

I'm trying to populate an array of bytes from a string that contains numbers(some being 2 digits) with spaces in between. I know there is an parseByte function but I'm unsure how to use that when populating an array as it seems to just take the string as one whole number.

my not-working-code:

public static void popArray(byte[][] array, String numbers)  
{  
    int counter = 0;        //counter to track position in string of data  
    for(int i=0; i<20; i++)         //cycle rows  
    {  
        for(int y=0; y<20; y++) //cycle columns  
        {  
        array[i][y] = (byte)(Character.digit(.charAt(counter), 10));    
        counter++;    //increase place in data string                                
        }  
    }  
}  
도움이 되었습니까?

해결책

Split the string at spaces, then iterate over the resulting array:

String[] parts = numbers.split("\\s+");
int counter = 0;                    //counter to track position in string of data  
for (int i = 0; i < 20; i++) {      //cycle rows  
    for (int y = 0; y < 20; y++) {  //cycle columns  
      String s = parts[counter];
      byte b = (byte)(Integer.parseInt())
      array[i][y] = b;    
      counter++;    //increase place in data string                                
    }  
}  
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top