-1

I am implementing a String matching algorithm for a username database. My method takes an existing Username database and a new username that the person wants and it checks to see if the username is taken. if it is taken the method is supposed to return the username with a number that isn't taken in the database.

Example:

"Justin","Justin1", "Justin2", "Justin3"

Enter "Justin"

return: "Justin4" since Justin and Justin with the numbers 1 thru 3 are already taken.

In my code sample below, newMember returns Justin1 even though it already exists--where is the mistake?

public class UserName {

    static int j = 0;

    static String newMember(String[] existingNames, String newName){
        boolean match = false;

        for(int i = 0; i < existingNames.length; i++){
            if(existingNames[i] == (newName)){
                match = true;
            }

        }
        if(match){
            j++;
            return newMember(existingNames, newName + j);
        }
        else{
            return newName; 
        }
    }

    public static void main(String[] args){

        String[] userNames = new String[9];
        userNames[0] = "Justin1";
        userNames[1] = "Justin2";
        userNames[2] = "Justin3";
        userNames[3] = "Justin";

        System.out.println(newMember(userNames, "Justin"));

        // I don't understand why it returns Justin1 when the name is already taken 
        // in the array.
    }
}
GamerJosh
  • 3,419
  • 3
  • 19
  • 28
user2510809
  • 235
  • 3
  • 14

9 Answers9

2

This line:

if(existingNames[i] == (newName)){

Should become

if(existingNames[i].equals(newName)){

In general, always use equals instead of == for Strings in Java.

krsteeve
  • 1,794
  • 4
  • 19
  • 29
1

I would change your approach to this. I think it would be better to use a Map<String, Integer>. For example, the fact that "Justin" - "Justin3" are taken usernames can be represented by the map:

{"Justin": 3}

To check if a username is taken, check if it's a key in the map. To get the "next" username for a specific taken username, get the value corresponding to the name from the map and add 1. Something like this:

static String newMember(Map<String, Integer> existingNames, String newName) {
    if (existingNames.containsKey(newName)) {
        int newNum = existingNames.get(newName) + 1;
        existingNames.put(newName, newNum);
        return newName + newNum;
    }

    existingNames.put(newName, 0);
    return newName;    
}

Oh, and use .equals() instead of == when comparing strings :)

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

You're not comparing the strings correctly, and you're adding the integer 1 to a string. Comparing strings is done with .equals. In order to concatenate the integer onto the end of the string:

static Integer j=0;

return newMember(existingNames, newName + j.toString());
Marco Corona
  • 812
  • 6
  • 12
0

When comparing strings in Java, you should generally use the equals() methods to do so.

tostao
  • 2,803
  • 4
  • 38
  • 61
Surveon
  • 729
  • 5
  • 12
0

*Never ever compare equality of Strings with ==. use equals() *

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Use .equals to compare strings in Java, not ==.

== will determine if the string references are the same which they almost certainly will not be. (There are rare cases when they may be due to the intern pool.)

.equals will determine if the content of the strings are the same.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
0

Using == with strings compares the references, not the underlying objects. Use str.equals().

blgt
  • 8,135
  • 1
  • 25
  • 28
0

I guess you have a function somewhere in your application answering if a username already exists, I call this usernameExists(String username) returning true or false.

String getOkUsername (String wantedUsername) {
  //if it's free, return it!
  if(!usernameExists(wantedUsername)) {
      return wantedUsername;
  };

  //OK, already taken, check for next available username 
  int i=1;
  while(usernameExists(wantedUsername+Integer.toString(i))) {
      i++;}
  return wantedUsername + Integer.toString(i);
}
claj
  • 5,172
  • 2
  • 27
  • 30
-1

It is much easier to do it in SQL:

select ... where ... LIKE username%
Germann Arlington
  • 3,315
  • 2
  • 17
  • 19