ユーザー名データベースの文字列一致アルゴリズムを実装しています。私の方法は、既存のユーザー名データベースと、その人が望む新しいユーザー名を取得し、ユーザー名が使用されているかどうかを確認します。それが取得された場合、メソッドはデータベースで取得されていない番号でユーザー名を返すことになっています。
例:
「ジャスティン」、「ジャスティン1」、「ジャスティン2」、「ジャスティン3」
「ジャスティン」と入力
return: "Justin4" は、1 から 3 までの数字を持つ Justin と Justin が既に使用されているためです。
以下のコード サンプルでは、newMember が null を返しますが、その理由はわかりません。「justin4」を返す必要があります
public class UserName {
static String newMember(String[] existingNames, String newName){
boolean found = false;
boolean match = false;
String otherName = null;
for(int i = 0; i < existingNames.length;i++){
if(existingNames[i].equals(newName)){
found = true;
break;
}
}
if(found){
for(int x = 1; x < 100 ; x++){
for(int i = 0; i < existingNames.length;i++){
if(existingNames[i].equals(newName + x))
match = true;
}
if(!match)
otherName = newName + x;
}
// It returns NULL instead of "Justin4". Its as if otherName doesn't
// change after its initialization.
return otherName;
} else return newName;
}
public static void main(String[] args){
String[] userNames = new String[4];
userNames[0] = "Justin1";
userNames[1] = "Justin2";
userNames[2] = "Justin3";
userNames[3] = "Justin";
System.out.println( newMember(userNames, "Justin"));
}
}