HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Integer to String recursive method

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
recursivestringintegermethod

Problem

I was experimenting with lists, sets, and finally maps when I spotted a pattern in my code to make it recursive. Now I haven't used recursion much in the past or at work and I was very excited to have something that does recursion.

I have this recursive method that takes in an Integer and returns a string representation of the Integer by reading two HashMaps.

For example representation(11) will be Eleven OR representation(89) will be Eighty-Nine, ect.

I have arranged to handle up to 999,999. Are there any improvements I could make to this current implementation to handle things more efficiently/speedily?

public String representation(Integer num) {
    StringBuffer numRep = new StringBuffer();
    StringBuffer hundred = new StringBuffer("Hundred");
    StringBuffer thousand = new StringBuffer("Thousand");
    //The case for anything less than 20; 0-19
    if (num  1000000){
        numRep.append("Sorry, this number is too large");
        JOptionPane.showMessageDialog(null, "Sorry, this number is too large\n"+"Integers 0 through 999,999 are exceptable.");
    }
    return numRep.toString();
}

Solution

-
Such an algorithm is very rarely used, so it is extremely unlikely that its performance matters. So, most chances are you do not need to optimize it at all. But if you insist:

-
Do not pre-initialize the 'hundred' and 'thousand' StringBuffers. Only initialize them if they are needed. As a matter of fact, it is good practice (though it does not have anything to do with performance) to not even declare them outside of the narrowest possible scope in which they will be used. By the way, I do not understand why 'hundred' and 'thousand' are StringBuffers instead of Strings.

-
You do not need HashMaps for this job; just use arrays of String. They will be faster.

-
It seems like the kind of recursion you are using is tail recursion, so you can trivially convert your function to work iteratively, which will probably yield better performance. I am not saying it will necessarily perform better, so write both versions and benchmark one against the other if you really care about performance. But if you do not, then keep the recursive version, it is way cooler, and even easier to read.

-
When you append to a StringBuffer, never append multiple strings concatenated with +. Always use a separate call to append() for each string. Otherwise, the compiler will very likely be creating more temporary StringBuffers behind your back in order to optimize your string append operations.

-
Considering that any number minus zero is still the same number, you can optimize this:

int temp = num % 1000;
if (temp == 0) {
    numRep.append(representation(num / 1000) + "-" + thousand);
} else {
    numRep.append(representation((num - temp) / 1000) + "-" + thousand
            + "-" + representation(temp));
}


into this:

int temp = num % 1000;
numRep.append( representation( (num - temp) / 1000) );
numRep.append( "-" );
numRep.append( thousand );
if( temp != 0 ) 
{
    numRep.append( "-" );
    numRep.append( representation( temp ) );
}

Code Snippets

int temp = num % 1000;
if (temp == 0) {
    numRep.append(representation(num / 1000) + "-" + thousand);
} else {
    numRep.append(representation((num - temp) / 1000) + "-" + thousand
            + "-" + representation(temp));
}
int temp = num % 1000;
numRep.append( representation( (num - temp) / 1000) );
numRep.append( "-" );
numRep.append( thousand );
if( temp != 0 ) 
{
    numRep.append( "-" );
    numRep.append( representation( temp ) );
}

Context

StackExchange Code Review Q#7369, answer score: 5

Revisions (0)

No revisions yet.