4
\$\begingroup\$

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.

asked Feb 4, 2015 at 14:58
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

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<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;
}
answered Feb 4, 2015 at 15:06
\$\endgroup\$
3
  • \$\begingroup\$ Many thanks to you,k you may have just saved me a massive headache when it comes to editing this code! \$\endgroup\$ Commented Feb 4, 2015 at 15:16
  • 1
    \$\begingroup\$ @BoltStorm be sure to also read the bugs section, before you get another headache \$\endgroup\$ Commented Feb 4, 2015 at 15:18
  • \$\begingroup\$ I already have and have accounted for any possible errors. \$\endgroup\$ Commented Feb 4, 2015 at 15:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.