Igor Tupitsyn
Igor Tupitsyn

Reputation: 1193

Java: implement a loop with duplicate values in Array

My task is to enter names into an array. If the name has already been entered, the program must alert about that and offer to reenter the player under the same number.

This is my code:

public void enterNames()    {

     for (int i=0; i<nameOfPlayers.length; i++) 
     {
           do 
           {    
               // isDuplicate is a Boolean initialized to false     
               System.out.println("CHECK": + isDuplicate);

               System.out.println("Enter player " + (i+1) + ":");

               nameOfPlayers[i] = in.next();

               for (int k=0; k<nameOfPlayers.length; k++)   
               {
                   if (k!=i && nameOfPlayers[i].equals(nameOfPlayers[k]))   
                   {
                       isDuplicate = true;                      
                       break;
                   }            
               }
           } while (isDuplicate = false);
      } 
 }

Interesting, even when I enter a duplicate value, it is caught and assigns true to isDuplicate, but when it returns to the beginning of the while loop, the value is false again ("CHECK: false").

Looks like an easy task, but I am caught...

Also, I did not want to use HashSet and wanted to use only Array.

Thanks a lot!

EDIT:

Thanks to others, I rewrote the code to the following:

public void enterNames()    {

    List<String> nameOfPlayersList = new ArrayList<String>();
    int i = 0;

    for (i=0; i<numberOfPlayers;)   
    {
        while(true) 
        {
            System.out.println("Enter player " + (i+1) + ":");
            String input = in.next();

            if(!nameOfPlayersList.contains(input))  
            {
                nameOfPlayersList.add(input);
                i++;
                break;
            }
            System.out.println("Player " + input + " already exists, please retry");
        }
    }
}

Upvotes: 0

Views: 1883

Answers (3)

Ankur Singhal
Ankur Singhal

Reputation: 26067

Answer reformed, used List to add more and more elements without pre defined size.

changed while (isDuplicate == false); to while (!isDuplicate);

 public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        List<String> nameOfPlayers = new ArrayList<String>();
        boolean isDuplicate = false;
        int i = 0;

        do {
            System.out.println("Enter player " + (i + 1) + ": or Q for Quit");
            String input = scanner.next();

            if (!input.equalsIgnoreCase("Q")) {
                if (nameOfPlayers.contains(input)) {
                    isDuplicate = true;
                } else {
                    nameOfPlayers.add(input);
                    isDuplicate = false;
                }
                System.out.println("CHECK : " + isDuplicate);
            } else {
                break;
            }

            i++;
        } while (!isDuplicate);
    }


Enter player 1: or Q for Quit
ankur
CHECK : false
Enter player 2: or Q for Quit
singhal
CHECK : false
Enter player 3: or Q for Quit
ankur
CHECK : true

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201439

Your while is incorrect, this

while (isDuplicate = false);

assigns false to isDuplicate which has a side-effect of also evaluating to false. You watned something like

while (isDuplicate == false);

or the shorter

while (!isDuplicate);

Upvotes: 1

Smalcat
Smalcat

Reputation: 231

The problem you are having is because of the

} while (isDuplicate = false);

it should be (mind the double ==)

} while (isDuplicate == false);

Apart from that your code is quite inefficient. You would probably do much better with two Arrays if that is really what you want, otherwise a linked list would be best.

Upvotes: 1

Related Questions