java - Extracting positive and negative numbers from char array -


i trying extract both positive , negative numbers character array (which coming file). apparently method wrote worked positive numbers. can please tell me how modify handle negative numbers well? let's character array

{'1',' ','2','-','2',' ','3','4'}

it should output list containing

{1,2,-2,34}.

some guidance appreciated. in advance

my method is:

private static arraylist<integer> extractintegers(char[] characters)  {     int beginning = -1;     arraylist<integer> numbers = new arraylist<integer>();         (int = 0; < characters.length; i++)             {                 boolean isnumber = character.isdigit(characters[i]);                     if (beginning != -1 && !isnumber) {                         numbers.add(parseintegers(characters, beginning, i));                         beginning = -1;                     } else if (beginning == -1 && isnumber) {                         beginning = i;                     }             }                     if (beginning != -1) {                         numbers.add(parseintegers(characters, beginning, characters.length));                     }     return numbers;   }  /*parses characters integers*/ private static int parseintegers(char[] characters, int begin, int end) {     return integer.parseint(new string(characters, begin, end - begin));   } 

try function like:

private static arraylist<integer> extractintegers(char[] characters) {     arraylist<integer> numbers = new arraylist<integer>();     char lastvalue = 49;//let int 1     (int = 0; < characters.length; i++) {         if (characters[i] == ' ') {             continue;         }         boolean isnumber = character.isdigit(characters[i]);//is digit?         boolean issign = (lastvalue == '-');//is last char value read '-'         if (isnumber) {             if (!issign)                 numbers.add(((int) characters[i] - 48));             else                 numbers.add((int) -1 * ((int) characters[i] - 48));//if last token "-" multiply -1         }         lastvalue = characters[i];     }     return numbers; } 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -