Android String.format disaster

wall-15995_640

I had a horrible bug in the Brainy Cell game. I used the String.format method without specifying the locale, despite all the warning spit out by Android Studio.

I actually have  a good excuse why I missed this issue: I implemented the whole game in about one week. The thing is that I load the levels from the asset folder and all levels are named something like c01l01 (chapter 01 level 01). To obtain the level name I used the String.format method as follows:

return String.format("c%02dl%02d", chapter, level);

Even though this code looks harmless it is actually really bad. If you don’t specify a locale, for an non-latin locale this will result in something like: c٠٢l٠١. Obviously this is not a valid level file name so the game crashes. The fix is simple, specify the locale:

return String.format(Locale.US, "c%02dl%02d", chapter, level);

This way the method will always return the same level name, regardless of the user locale.

Don’t forget to check out my Android games: Brainy Cell, Facts No Yes, Snowflakes. Also, if you found this post useful, please feel free to share + give it a LIKE, so others can find it too! Thanks 😊

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s