Question

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                                
        }  
    }  
}  
Était-ce utile?

La solution

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                                
    }  
}  
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top