for my compsci assignment we're supposed to take a string given to us with integers and letters in it, then create a method that takes that string and converts it into an integer array with the integers in it. For some reason my method is not adding ints to the array, I'm not sure why.
For the LETTERS given in the string, we're supposed to discard them, so we have an array with ONLY int values; ex. input: abs3131afas312 the array would have {3131,312}
This is the link to the assignment.
Here's my method:
public static int[] intParse(String a){
int[] array1 = new int[a.length()];
int b = 0;
for(int i = 0; i < a.length(); ++i)
{
int g = a.charAt(i);
if(g == 1 || g == 2 || g == 3 || g == 4 || g == 5 || g == 6 || g == 7 || g == 8 || g == 9 || g == 0)
{
String c;
for(int j = i; j < a.length(); ++j)
{
int k = a.charAt(j);
if(k != 1 && k != 2 && k != 3 && k != 4 && k != 5 && k != 6 && k != 7 && k != 8 && k != 9 && k != 0)
{
c = a.substring(j,k-1);
array1[b] += Integer.parseInt(c);
b++;
j = (a.length());
i = a.charAt(j);
}
else
{
c = a.substring(j,a.length());
array1[b] = Integer.parseInt(c);
j = a.length();
}
}
}
}
return array1;
}
if g == '1'
, orCharacter.isDigit(...)
. – MouseEvent Dec 11 '12 at 1:43