1

I'm a little bit stuck on this one. I'm trying to compare a String against a jsonobject.getString and it seems whatever I do they do not match:

String date = scoreJson.getString("Date");
if (dateString.equals(date))
{
    //do stuff
}

The value of dateString is assigned here:

calendar = Calendar.getInstance();
int day = calendar.get(calendar.DAY_OF_MONTH), month = calendar.get(calendar.MONTH), year = calendar.get(calendar.YEAR);

//months start at 0 in calendar
month += 1;

//use builder to create the dateString
builder.append(year).append("-").append(month).append("-").append(day);
dateString = builder.toString();

When I view both Strings in debug they show "2015-05-18", however they will not match in a conditional statement. Does anyone have any idea why, and how I could compare them?

Thanks.

Edit: The JSON object will come back in this format:

{"ScoreData":{"Username":"testUser","Id":"8fb25209-863a-410b-a440-
b5b57a903ee1","Date":"2015-02-25","Score":"25.3"}}

I am retrieving the values from it here and saving to sharedprefs:

//response comes back as two JSONObjects, this makes the inner object into a new JSON
JSONObject responseJson = new JSONObject(response);

JSONObject scoreJson = new JSONObject(responseJson.getString("ScoreData"));

String date = scoreJson.getString("Date");
puzzleScore = scoreJson.getString("Score");

SharedPreferencesWrapper.saveToPrefs(c, "score-" + date, puzzleScore);

I am then checking for the key here:

puzzleScore = SharedPreferencesWrapper.getFromPrefs(c, "score-" + dateString, "NotFound");

if (!puzzleScore.contains("NotFound"))
{
    //do stuff
}
9
  • Maybe you have non-printable characters in the string. Also why not compare Date instances instead? Commented May 18, 2015 at 21:03
  • Json strings include " so try replaceAll("\"", "") before comparing. Commented May 18, 2015 at 21:04
  • @AlexisC. Hmm it's possible, I'm using String compares because the place in my code where I need to compare them is to retrieve an item from sharedpreferences, for example: code SharedPreferencesWrapper.saveToPrefs(c, "score-" + date, puzzleScore); code puzzleScore = SharedPreferencesWrapper.getFromPrefs(c, "score-" + dateString, "NotFound"); Commented May 18, 2015 at 21:07
  • Comparing equality by toString() representation is ugly. toString() is usually used for representation, not comaprison. Better idea is to parse date obtained from JSON to Calendar and compare Calendar objects. Commented May 18, 2015 at 21:10
  • @FilipMalczak. Unfortunately I can't parse to a date as I need the two Strings to match to pull the key from sharedpreferences. Commented May 18, 2015 at 21:16

1 Answer 1

1

Have you tried adding toString() after this scoreJson.getString("Date"), so the code would be like scoreJson.getString("Date").toString().

I think it is a problem with encoding. If that doesn't work, check for whitespaces around the string. You can remove starting & trailing whitespaces with trim().

Hope this helps, good luck~

Sign up to request clarification or add additional context in comments.

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.