Sunday, December 02, 2007

Fun With Strings

Here are some of my solutions to Java exercises. Like before, the formatting is way off on these, because Blogger doesn't support code tags. Trust me when I say that I have the formatting done perfectly (using Ctrl+Shift+F on NetBeans, LOL).

These wake me during early mornings and make me sleepy during the day (but not so sleepy to cause work disruption, heh). Why do I post these on my blog?

I'm just trying to be helpful. I'm sure that I'm not alone in plodding along, learning Java. Why waste blog space ranting about performance issues in the office when you can make someone's day by posting a solution to a problem that has been bothering them for weeks? You have the search function for that. Hehehe.

Besides, the few people that actually come here for the Java code can always craft their own code using a different implementation. That's what I love about learning programming - different approaches work.

It's just like some aspects of mathematics. I loved it when I knew how to get to the answer without really going through the book. During high school, I was one of a few who actually got the matrices and some of the more advanced trigonometry lessons. (All modesty aside.)

Anyway, my head's in the clouds again so here are three problems to keep you busy. My advice is to install an IDE and reformat my code. NetBeans would be fine if you intend to make Java a career, and be Sun certified or something.

I'll be posting a more sobering article next time. For now, at 3AM on a weekend, I don't really feel like ranting about my difficulties at work... because that's the only newsworthy thing I can blog about right now.
8.5** (Occurrences of each digit in a string) Write a method that counts the occurrences of each digit in a string using the following header:
public static int[] count(String s)

The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int[] counts = count("12203AB3"), counts[0] is 1, counts[1] is 1, counts[2] is 2, counts[3] is 2.

Write a main method to display the count for "SSN is 343 32 4545 and ID is 434 34 4323".
public class CountDigitOccurrence {
public static int[] count(String s) {
int[] count = new int[10];

for (int j = 0; j < count.length; j++){
for (int i = 0; i < s.length(); i++){
if (s.charAt(i) == 48 + j)
return count;

public static void main(String[] args) {
String digits = "15749873413";
int[] count = CountDigitOccurrence.count(digits);

for (int k = 0; k < count.length; k++)
System.out.println(k + " is found " + count[k] + " times in " + digits);

String ssn = "SSN is 343 32 4545 and ID is 434 34 4323";
int[] count2 = CountDigitOccurrence.count(ssn);

for (int k = 0; k < count2.length; k++)
System.out.println(k + " is found " + count2[k] + " times in \"" + ssn + "\"");


8.9** (Decimal to hex) Write a method that parses a decimal number into a hex number as a string. The method header is as follows:
public static String convertDecimalToHex(int value)

Use decimal 298 and 9123 to test the method.
public class DecimalToHex {
public static String convertDecimalToHex(int value) {
StringBuffer hexOutput = new StringBuffer();
int divisor = value;

do {
char digit = (char)(divisor % 16);
if ((int)digit > 9)
digit = (char)('A' + (digit - 10));
digit = (char)('0' + digit);
int newDivisor = divisor / 16;
divisor = newDivisor;
} while (divisor > 16);

char lastDigit = (char)(divisor % 16);
if ((int)lastDigit > 9)
lastDigit = (char)('A' + (lastDigit - 10));
lastDigit = (char)('0' + lastDigit);

return hexOutput.substring(0);

public static void main(String[] args) {
int value1 = 298;
String hexValue1 = convertDecimalToHex(value1);
System.out.println(value1 + "'s hex value is " + hexValue1);
int value2 = 9123;
String hexValue2 = convertDecimalToHex(value2);
System.out.println(value2 + "'s hex value is " + hexValue2);
8.11** (Sorting characters in a string) Write a method that returns a sorted string using the following header:
public static String sort(String s)

For example, sort("acb") returns abc.
public class SortCharsInString {
public static String sort(String s) {
StringBuffer s2 = new StringBuffer(s);
char[] s2charArray = new char[s2.length()];
for (int i = 0; i < s2charArray.length; i++) {
s2charArray[i] = s2.charAt(i);


StringBuffer newS2 = new StringBuffer(s2.length());
return newS2.substring(0);
public static void main(String[] args) {
String sortedString = sort("asdfghjklqwertyuiopzxcvbnm");

No comments: