0

I have the following string with characters, numbers and also extra non-numeric characters. I managed to replace the non-numeric characters. But I am lost with characters and numbers at the end.I am getting this string from the following method. I use this method to remove the non-numeric.

String[] stringArray = string.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("[^ A-Za-z0-9]","").trim().split(",");

results with "I7f253e0", "I6fc2699". How can I parse this ?

public static int[] string_to_int_arr(String string){

    String[] stringArray = string.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("[^ A-Za-z0-9]","").trim().split(",");

    int[] intArray = new int[stringArray.length];

    for (int i = 0; i < stringArray.length; i++){
        try {
            intArray[i] = Integer.parseInt(stringArray[i]);
        } catch (NumberFormatException e){
            Log.e(TAG,"" + e);
        }
    }
    return intArray;
}
cantona_7
  • 1,127
  • 4
  • 21
  • 40
  • 1
    Remove all your manipulations and use Arrays.toString(myarray) – azro Oct 31 '19 at 09:51
  • manipulations ? you mean. can you be more specific please :) – cantona_7 Oct 31 '19 at 09:54
  • 1
    Are you sure it's normal to have this kind of String in the first place? This looks like an object reference. I can't see what anyone could do with a modified string of a reference – jhamon Oct 31 '19 at 09:54
  • 1
    The string you use at the beginning , what is it ? where does it come from ? It shouldn't be like this – azro Oct 31 '19 at 09:55
  • @azro i've just updated my question. please have a look – cantona_7 Oct 31 '19 at 09:59
  • That still doesn't give an answer to "are you sure that the kind of String to expect?". It adds even more questions. Like why does loging `"string1"+ String1` gives `[I@7f253e0`. How does the part "string1"`` disappear? Is the`targ(String[] string)` parameter correctly filled (i.e not filled with with a `myArray.toString()`) – jhamon Oct 31 '19 at 10:05
  • It is still not clear where this strings come from or if the numbers you are trying to parse are really the data you need. It looks like as if you are doing `String x = MyClass.toString()` but your MyClass dosen't overide a toString method. – Eritrean Oct 31 '19 at 10:05
  • @jhamon I am trying to fill the `targ(String[] string)` array with the values. and extract the string stored in `string[8 + i]` index. And convert the string to int array – cantona_7 Oct 31 '19 at 10:10

1 Answers1

1

I don't think it will help, but anyway, if you want to remove all non digits just do the following:

String x = "[I@7f253e0";
String y = x.replaceAll("\\D", "");
System.out.println(y);

//72530
Eritrean
  • 15,851
  • 3
  • 22
  • 28