You’ve read about how Strings work in Java. Test your new skills with this challenge: solve this String-based puzzle to help you solidify your knowledge of the Java programming language and Android development. That's right, Android, too! You may need to refer to other Android tutorials here.
To prepare for this challenge, you’ll want to start with a basic Android application. Simply create an Android application within Eclipse and edit its default Activity, specifically the onCreate() method, to test the code you create for this challenge. Remember, you can use the Log.v() method to print output (including strings) to the debug log. This is a quick and convenient way to display testing output for exercises such as this one.
If what we've just asked of you is already too challenging, we would recommend taking a step back. Start with some of the Android tutorials, such as Introduction to Android Development or Beginning Android: Getting Started with Fortune Crunch. Once you’ve mastered setting up an Android project, return and try these progressive exercises.
A Progressive Challenge
This is a progressive challenge. If you follow each of the steps below correctly, you will solve a puzzle. Keep in mind, there are numerous ways to approach this problem and therefore numerous solutions.
You can print out your progress to the Android debug log called LogCat using the Log.v() method.
Bonus: Extra points (not that you’re being graded) for those who catch the reference to this puzzle, and where it comes from.
Step 1: Define Your String
For your first step, you’ll need to define a new text string for use in this challenge. You’ve got lots of options here. For the many ways in which you can define strings in Java, see our String tutorial. The simplest method, of course, would be to simply define a new variable of type String.
The contents of this string should be the following text:
Step 2: Convert to Uppercase
For your next trick, you’ll want to convert the string to all uppercase letters.
Hint: If you’re stuck, check the methods available within the String class, as defined in the Android SDK documentation.
Sanity Check: The contents of your new string should be the following text: SETEC ASTRONOMY
Step 3: Rearrange The Letters
Now things will get a bit more challenging…
As you may recall, a string is simply a sequence of characters. In this step, you want to rearrange the characters in your uppercase string and add one extra character.
Think of your current string as a 0-based array of characters. Now rearrange the characters of your string into the following sequence, where each number represents the current character index, dashes are used to separate each index, and the asterisk (*) is used to represent a space character:
Hint: Remember that String variables cannot be changed. Seriously consider using StringBuilder.
to build up your new string contents from the specific characters of your source string.
Sanity Check: If you print out the resulting string, it should be:
STERCES YNAM OOT.
Step 4: Reverse Those Letters
You’re almost there! The only thing left to do is to reverse the characters in your string. Hopefully you used a StringBuilder to build up your string from individual characters; it’s got a helpful method that can this job for you.
Print the resulting string. It should be clear if you’ve solved the puzzle.
Step 5: Tweak Your Solution (Optional Challenge)
It’s always a good idea to review your solution and improve it, perhaps making it slightly more elegant or flexible. While this step is certainly not essential to solving the puzzle (especially as you’ve already done so), we suggest you continue if you’ve found the previous steps reasonably straightforward.
Assuming you haven’t already, consider how you might define a String variable to represent the index sequence discussed in Step 3.
String strSequence = "7-8-3-9-4-1-0-*-14-11-6-13-5-12-10-2";
Now, take the following steps:
Parse the string above into an array of Strings, one for each number or asterisk.
Hint: Check the String class method called split().
- Create a loop and iterate through this array of strings and build the resulting string using a StringBuilder. As part of this process, you will need to convert each String to the appropriate integer, or to a space character.
Android developers use Strings all the time to store textual data for various purposes. Strings often need to be manipulated in order to be used within your code. As always, there are many ways to solve these problems, so feel free to post your alternative answers (or any questions) in the comments section. We have supplied a couple of methods in the accompanying attachment.
Best of luck!
About the Authors
Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams TeachYourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to email@example.com, via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.
Need More Help Writing Android Apps? Check out our Latest Books and Resources!
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post