Android String.format disaster
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.
Get Toth Arpad’s stories in your inbox
Join Medium for free to get updates from this writer.
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. If you found this post useful, please feel free to share + give it ❤️, so others can find it too! You can also check my personal page at https://arpytoth.com. Thanks! 😊








