patternjavaMinor
Java CharSequence iteration
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:
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.
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,
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:
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
But school said I can't use those methods!
Oh. Okay.
First, store
Then, instead of
After all,
See,
Bugs
One of the things you need to be aware of is that your function can get a
You should check if the string you get is null or empty:
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, CharSequenceSee,
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>, CharSequenceif (fName == null || fName.length == 0){
return false;
}Context
StackExchange Code Review Q#79584, answer score: 4
Revisions (0)
No revisions yet.