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

Java CharSequence iteration

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

Problem

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.

Solution

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... https://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, 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;
}

Code Snippets

return Character.isDigit(myString.charAt(0));
String allowedChars = "0123456789";
String firstChar = fName.substring(0,1);
return allowedChars.contains(firstChar);
public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence
if (fName == null || fName.length == 0){
    return false;
}

Context

StackExchange Code Review Q#79584, answer score: 4

Revisions (0)

No revisions yet.