Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

A project I am working on requires me to check the first character of an input string, to determine if it is a numeric character. I have developed the following code:

public static boolean IsLeadingCharNumfName(String fName)
{
    CharSequence[] numbs;
    numbs  = new CharSequence[10];
    numbs[0] = "0";
    numbs[1] = "1";
    numbs[2] = "2";
    numbs[3] = "3";
    numbs[4] = "4";
    numbs[5] = "5";
    numbs[6] = "6";
    numbs[7] = "7";
    numbs[8] = "8";
    numbs[9] = "9";

    if(fName.substring(0,1).equals(numbs[0]))
    {
        return true;
    }
    if(fName.substring(0,1).equals(numbs[1]))
    {
        return true;
    }
    if(fName.substring(0,1).equals(numbs[2]))
    {
        return true;
    }
    if(fName.substring(0,1).equals(numbs[3]))
    {
        return true;
    }
    if(fName.substring(0,1).equals(numbs[4]))
    {
        return true;
    }
    if(fName.substring(0,1).equals(numbs[5]))
    {
        return true;
    }
    if(fName.substring(0,1).equals(numbs[6]))
    {
        return true;
    }
    if(fName.substring(0,1).equals(numbs[7]))
    {
        return true;
    }
    if(fName.substring(0,1).equals(numbs[8]))
    {
        return true;
    }
    if(fName.substring(0,1).equals(numbs[9]))
    {
        return true;
    }
    else
    {
        return false;
    }
}

I feel that this code can be optimised for efficiency, but I'm not sure how. I'll have to do the same for checking if the name contains a number.

What I am looking for, is a way to lower the code footprint primarily, with efficiency as a secondary bonus.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Use Java's built-in functions for things like this. Specifically, Character.isDigit(char c) and String.charAt(int index).

What you have right now is a poor reimplementation that makes any maintenance programmer go "wait, what?".

It sounds mean, but consider what it actually is:

return Character.isDigit(myString.charAt(0));

That one line is all you needed.

To prevent things like this happening in the future, I suggest googling your problem ("java string starts with digit" or "java string starts with number").

You'd have arrived at this question... http://stackoverflow.com/questions/1107798/how-to-check-a-string-starts-with-numeric-number .. which also points you to Character.isDigit. A little bit of google will go a long way in your programming work.

But school said I can't use those methods!

Oh. Okay.

First, store fName.substring(0,1) in a temporary variable. No need to substring forever. Then, instead of Character.isDigit...

String allowedChars = "0123456789";
String firstChar = fName.substring(0,1);
return allowedChars.contains(firstChar);

After all, String IS CharSequence. You can see this in the documentation of java.lang.String.

public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence

See, String implements CharSequence.

Bugs

One of the things you need to be aware of is that your function can get a null string or an empty string.

You should check if the string you get is null or empty:

if (fName == null || fName.length == 0){
    return false;
}
share|improve this answer
    
Many thanks to you,k you may have just saved me a massive headache when it comes to editing this code! –  BenignReaver Feb 4 at 15:16
1  
@BoltStorm be sure to also read the bugs section, before you get another headache –  Pimgd Feb 4 at 15:18
    
I already have and have accounted for any possible errors. –  BenignReaver Feb 4 at 15:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.