0

I have an php api which give me array like this:

11%0%0%801%0%0%812%0%0%640%33%6

Above response is going to be stored in "String a"

try{
    //... Some code of my application which is not relavant to show
    String a = response.toString();
    String tokens = a.split("%");
    for (String str : tokens)
    {
        Log.e("log_tag", str);
    }
    String ds = tokens[0];
    int i_ds = Integer.parseInt(ds);
    Log.e("log_tag", "data is: "+i_ds);
}catch (Exception e) {
     Log.e("log_tag","Error: " + e.toString());
}

I am getting following in my DDMS:

Tag         Text
log_tag     11
log_tag     0
log_tag     0
log_tag     801
log_tag     0
log_tag     0
log_tag     812
log_tag     0
log_tag     0
log_tag     640
log_tag     33
log_tag     6
log_tag     java.lang.NumberFormatException: Invalid int:"11"
1
  • 3
    How does this compile? Commented Aug 25, 2013 at 21:54

2 Answers 2

3

I Just copied and pasted your code, although other comments do state the truth that you do need to make this change: String [] tokens = a.split("%");

Looks like php is passing you some fun hidden characters! Theres a hidden character before the number 11, I would sanitize your string by doing the following

tokens[0].replaceAll("[^0-9]", "");
Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately it would not, trim() only removes spaces (" ") not hidden characters, its a series of UTF8 chars leading his string getting passed from the php code. My eclipse tells me so!
I checked method trim(). It removes white characters, so \n,\t also.
Well, I dont know what to tell you other than I copied and pasted his code, ran it and trim did not work....
So it seems, that it is different white character. Nevermind, you code is working.
Thanks @ns47731 , could you please tell me more about these hidden characters?
|
0

String.split returns a String[]

Replace String tokens = a.split("%"); with String[] tokens = a.split("%");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.