Hey i was trying to recognize numbers from a string and convert them into a String array, but with this code what i get is:
IE: [null1, null2, exc..]
I dont know how to take out this null value.
I also would like to find a way to do this, without an array, because i should increase its length manually.
Any tips? Thanks!
public class ProvaToArray {
public static void main(String[] x) {
String s = "1-2-3-4-lorem23ip567um1";
String[] ris = toArray(s);
System.out.println(Arrays.toString(ris)); //should printout [1, 2, 3, 4, 23, 567, 1]
}
public static String[] toArray(String s){
String[] l = new String[10];
int count = 0;
for(int i = 0; i < s.length() - 1; i++){
if(estNumber(s.charAt(i) + "")){
l[count] += "" + s.charAt(i);
if(!estNumber(s.charAt(i+1) + "")){
count++;
}
}
if(i+1 == s.length()-1){
if(estNumber(s.charAt(i+1) + "")){
l[count] = "" + s.charAt(i+1);
}
}
}
return l;
}
public static boolean estNumber(String i){
if(i.matches("^-?\\d+$"))
return true;
else
return false;
}
}